﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using System.Data.Entity;
using System.Reflection;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using Anke.SHManage.IDAL;

namespace Anke.SHManage.MSSQLDAL
{
    public partial class M_PatientRecordDAL : IM_PatientRecordDAL
    {
        IDALContext dalContext = new DALContextFactory().GetDALContext();
        AKV7ManageEntities db = DBContextFactory.GetDBContext() as AKV7ManageEntities;

        #region 查询方法

        #region 出车信息

        #region 出车信息--填写病历列表
        public object GetTasks(int page, int rows, string order, string sort, DateTime m_BeginTime, DateTime m_EndTime
           , string m_LinkPhone, int m_AlarmEventType, string m_LocalAddr, string m_TaskResult, string m_TaskAbendReason
           , string m_CenterCode, string m_StationCode, string m_AmbCode, string m_Driver, string m_Doctor, string m_Litter, string m_IsCharge
           , string m_IsFill, string m_Nurse, string m_PatientName, string searchBound, string isTest, M_UserLoginInfo loginInfo
           , string m_CPRIFSuccess, string m_PatientState, string fapiaoHao, string illState, string SendAddress
           , string isYBPay)
        {
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(SqlHelper.MainConnectionString);//取管理库的链接字符串
                SqlConnectionStringBuilder builderyb = new SqlConnectionStringBuilder(SqlHelper.YBConnectionString); //add2021-10-19 plq 医保上线移植
                StringBuilder sbSQL = new StringBuilder();//sql语句
                sbSQL.Append("declare @BeginTime datetime ");
                sbSQL.Append("declare @EndTime datetime ");
                sbSQL.Append("set @BeginTime='" + m_BeginTime + "' ");
                sbSQL.Append("set @EndTime='" + m_EndTime + "' ");
                sbSQL.Append("declare @TaskCodeB char(20) ");
                sbSQL.Append("declare @TaskCodeE char(20) ");
                sbSQL.Append("set @TaskCodeB = convert(char(8),@BeginTime,112)+'000000000000' ");
                sbSQL.Append("set @TaskCodeE = convert(char(8),dateadd(day,1,@EndTime),112)+'000000000000' ");

                sbSQL.Append("SELECT  t.任务编码,ae.首次受理时刻,acc.联系电话, 事件类型=tzae.名称");
                sbSQL.Append(",acc.患者姓名,acc.现场地址,acc.患者人数,派车时刻=t.生成任务时刻,受理类型=tzac.名称,s.中心编码,t.分站编码,");
                sbSQL.Append("分站名称=s.名称,amb.实际标识,");
                //sbSQL.Append("分站名称=s.名称,amb.实际标识,司机=dbo.GetStr(t.任务编码,3),医生=dbo.GetStr(t.任务编码,4),担架工=dbo.GetStr(t.任务编码,6),护士=dbo.GetStr(t.任务编码,5),");
                sbSQL.Append("出车结果=case when t.是否正常结束=1 then '正常' else '异常结束' end, ");
                //病历数
                sbSQL.Append("病历数= (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord ");
                sbSQL.Append(" p WHERE p.TaskCode=t.任务编码) ");
                //收费-添加多收费后 修改为有一个欠费则显示欠费
                //sbSQL.Append(",收费数=isnull((SELECT 收费数=case when sum(case when 是否欠费=1 then 1 else 0 end)>0  then '欠费' else '已收' end FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 group by TaskCode ),'未收')  ");
                sbSQL.Append(@"into #tempX 
                        FROM TTask t 
                        JOIN TAcceptEvent acc ON t.事件编码=acc.事件编码 AND t.受理序号=acc.受理序号 
                        JOIN TAlarmEvent ae ON t.事件编码=ae.事件编码 
                        LEFT JOIN TStation s ON t.分站编码=s.编码 
                        LEFT JOIN TAmbulance amb ON t.车辆编码=amb.车辆编码 
                        LEFT JOIN TZAlarmEventType tzae ON tzae.编码=ae.事件类型编码 
                        LEFT JOIN TZAcceptEventType tzac ON tzac.编码=acc.受理类型编码 ");
                StringBuilder sbWhereClause = new StringBuilder();
                sbWhereClause.Append("where 1=1  ");
                //sbWhereClause.Append("where t.任务编码>@TaskCodeB and t.任务编码<=@TaskCodeE  ");
                WhereClauseUtility.AddDateTimeGreaterThan("t.生成任务时刻", m_BeginTime, sbWhereClause);
                WhereClauseUtility.AddDateTimeLessThan("t.生成任务时刻", m_EndTime, sbWhereClause);
                //WhereClauseUtility.AddDateTimeGreaterThan("ae.首次受理时刻", m_BeginTime, sbWhereClause);
                //WhereClauseUtility.AddDateTimeLessThan("ae.首次受理时刻", m_EndTime, sbWhereClause);
                WhereClauseUtility.AddStringEqual("ae.是否测试", isTest, sbWhereClause);//是否测试
                //WhereClauseUtility.AddStringLike("ae.患者姓名", m_PatientName, sbWhereClause);//患者姓名--20151125调整从病历表取
                WhereClauseUtility.AddStringLike("acc.联系电话", m_LinkPhone, sbWhereClause);
                WhereClauseUtility.AddIntEqual("ae.事件类型编码", m_AlarmEventType, sbWhereClause);
                WhereClauseUtility.AddStringLike("acc.现场地址", m_LocalAddr, sbWhereClause);
                WhereClauseUtility.AddStringEqual("t.是否正常结束", m_TaskResult, sbWhereClause);
                //WhereClauseUtility.AddIntEqual("t.异常结束原因编码", m_TaskAbendReason, sbWhereClause);//20160328换为取病历空车原因
                WhereClauseUtility.AddStringEqual("t.车辆编码", m_AmbCode, sbWhereClause);
                WhereClauseUtility.AddStringLike("t.用户流水号", m_Litter, sbWhereClause);//20151216担架工修改为流水号
                //WhereClauseUtility.AddStringEqual("ae.中心编码", loginInfo.DispatchSubCenterID, sbWhereClause);//20210604
                if (m_IsCharge == "True")
                {
                    if (sbWhereClause.Length > 0)
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.IFArrearage='False') >0");
                    else
                        sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.IFArrearage='False') >0");
                }
                else if (m_IsCharge == "False")
                {
                    if (sbWhereClause.Length > 0)
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码) =0");
                    else
                        sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码) =0");
                }
                //20150914-增加应收与实收不符
                else if (m_IsCharge == "Different")
                {
                    if (sbWhereClause.Length > 0)
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.ReceivableTotal!=cr.PaidMoney) >0");
                    else
                        sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.ReceivableTotal!=cr.PaidMoney) >0");
                }
                //20151215-增加重开发票
                else if (m_IsCharge == "AgainInvoice")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientChargeLog cr Where cr.TaskCode=t.任务编码 and cr.Isvalid=0) >0");
                }
                //20160427-增加应收与实收相符
                else if (m_IsCharge == "Match")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.ReceivableTotal=cr.PaidMoney) >0");
                }
                //else if (m_IsCharge == "Qian")
                //{
                //    if (sbWhereClause.Length > 0)
                //        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.IFArrearage='True') >0");
                //    else
                //        sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge cr Where cr.TaskCode=t.任务编码 and cr.IFArrearage='True') >0");
                //}

                //20160328--异常结束原因取病历的空车原因和送往地点的“车到不送”--20161124修改为“车到未送”
                if (m_TaskAbendReason != "-1")
                {
                    if (m_TaskAbendReason == "车到未送")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.SendAddress like '%" + m_TaskAbendReason + "%' )>0 ");
                    else
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.EmptyReason= '" + m_TaskAbendReason + "' )>0 ");
                }

                ////20160408--增加发票号查询
                if (fapiaoHao != "")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientCharge pc WHERE pc.TaskCode=t.任务编码 and pc.InvoiceNumber like '%" + fapiaoHao + "%' )>0 ");
                }

                ////20160518--增加病情分类查询
                if (illState != "-1")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.IllnessClassification = '" + illState + "' )>0 ");
                }
                ////20160531--增加送往地点查询
                if (SendAddress != "-1")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.SendAddress like '%" + SendAddress + "%' )>0 ");
                }

                //20210625 sr 增加是否医保收费  //add2021-10-19 plq 医保上线移植
                if (isYBPay != "-1")
                {
                    if (isYBPay == "0")
                    {

                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builderyb.InitialCatalog).Append(".dbo.M_PatientRecordYiBao mpryb WHERE mpryb.任务编码=t.任务编码 and mpryb.操作状态 in (3,4) )= 0 ");
                    }
                    else if (isYBPay == "1")
                    {
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builderyb.InitialCatalog).Append(".dbo.M_PatientRecordYiBao mpryb WHERE mpryb.任务编码=t.任务编码 and mpryb.操作状态 in (3,4) ) > 0 ");
                    }
                }

                //20151125--调整患者姓名从病历查询
                if (m_PatientName != "")
                {
                    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.Name like '%" + m_PatientName + "%' )>0 ");
                }
                if (m_IsFill != "-1")
                {
                    //20151125--调整病历审核情况内容
                    if (m_IsFill == "中心未抽查")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.CenterIFSpotChecks=0 )>0 ");
                    else if (m_IsFill == "中心已抽查")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.CenterIFSpotChecks=1 )>0 ");
                    else if (m_IsFill == "中心抽查待定")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.CenterSpotChecksResult='待定') )>0 ");
                    else if (m_IsFill == "中心抽查优秀")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.CenterSpotChecksResult='优秀') )>0 ");
                    else if (m_IsFill == "中心抽查良好")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.CenterSpotChecksResult='良好') )>0 ");
                    else if (m_IsFill == "中心抽查合格")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.CenterSpotChecksResult='合格') )>0 ");
                    else if (m_IsFill == "中心抽查未通过")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.CenterSpotChecksResult='未通过') )>0 ");
                    else if (m_IsFill == "中心抽查进步")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.IFMedicalHistoryWritingProgress=1) )>0 ");
                    else if (m_IsFill == "分站未抽查")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.SubCenterIFSpotChecks=0 )>0 ");
                    else if (m_IsFill == "分站已抽查")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.SubCenterIFSpotChecks=1 )>0 ");
                    else if (m_IsFill == "分站抽查待定")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.SubCenterSpotChecksResult='待定') )>0 ");
                    else if (m_IsFill == "分站抽查优秀")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.SubCenterSpotChecksResult='优秀') )>0 ");
                    else if (m_IsFill == "分站抽查良好")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.SubCenterSpotChecksResult='良好') )>0 ");
                    else if (m_IsFill == "分站抽查合格")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.SubCenterSpotChecksResult='合格') )>0 ");
                    else if (m_IsFill == "分站抽查未通过")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and (p.SubCenterSpotChecksResult='未通过') )>0 ");
                    else if (m_IsFill == "已通知参加小讲课")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and ((p.IFNoticeAttendNursingLectures=1) or (p.IFFillNoticeAttendNursingLectures=1) or (p.IFFill2NoticeAttendNursingLectures=1)) )>0 ");
                    else if (m_IsFill == "实际参加小讲课未填写")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and ((p.IFNoticeAttendNursingLectures=1 and p.ActualAttendNursingLectures is null) or (p.IFFillNoticeAttendNursingLectures=1 and p.FillActualAttendNursingLectures is null) or (p.IFFill2NoticeAttendNursingLectures=1 and p.Fill2ActualAttendNursingLectures is null)) )>0 ");
                    else if (m_IsFill == "实际小讲课缺席参加")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and ((p.IFNoticeAttendNursingLectures=1 and p.ActualAttendNursingLectures='缺席参加') or (p.IFFillNoticeAttendNursingLectures=1 and p.FillActualAttendNursingLectures='缺席参加') or (p.IFFill2NoticeAttendNursingLectures=1 and p.Fill2ActualAttendNursingLectures='缺席参加')) )>0 ");
                    //else if (m_IsFill == "一般" || m_IsFill == "优秀")
                    //    sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode=1 and (p.SubCenterSpotChecksResult='" + m_PatientState + "' or p.CenterSpotChecksResult='" + m_PatientState + "') )>0 ");

                }
                if (m_PatientState != "-1")
                {
                    //20150915-增加病历状态下拉内容
                    if (m_PatientState == "True")
                    {
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码)>0");
                    }
                    else if (m_PatientState == "False")
                    {
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码)=0 ");
                    }
                    else if (m_PatientState == "1" || m_PatientState == "0" || m_PatientState == "2" || m_PatientState == "3")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode='" + m_PatientState + "')>0 ");
                    else if (m_PatientState == "已回访")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p LEFT JOIN ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR cpr on p.TaskCode=cpr.TaskCode and p.PatientOrder=cpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess!='ROSC' and (cpr.DoctorFollowUp!='' or cpr.DoctorFollowUp!=null or DoctorFollowUpReason!='' or DoctorFollowUpReason!=null) )>0 ");
                    else if (m_PatientState == "未回访")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p LEFT JOIN ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR cpr on p.TaskCode=cpr.TaskCode and p.PatientOrder=cpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess!='ROSC' and (cpr.DoctorFollowUp='' or cpr.DoctorFollowUp=null or DoctorFollowUpReason='' or DoctorFollowUpReason=null) )>0 ");
                    else if (m_PatientState == "自动归档")
                    {
                        string TimePatientRecordNotUp = DateTime.Now.AddHours(-AppConfig.GetConfigInt("TimePatientRecordNotUp")).ToString("yyyy-MM-dd HH:mm:ss");
                        sbWhereClause.Append(" AND t.生成任务时刻<='" + TimePatientRecordNotUp + "' AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode=0 and isnull(p.UpdateFormCompleteTime,0)<='" + TimePatientRecordNotUp + "')>0 ");
                    }
                    //else
                    //    sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode='" + m_PatientState + "')>0 ");
                }
                if (m_CPRIFSuccess != "-1")
                {
                    if (m_CPRIFSuccess == "ROSC")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess='" + m_CPRIFSuccess + "')>0 ");
                    else if (m_CPRIFSuccess == "已审核")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p left join ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR prcpr on p.TaskCode=prcpr.TaskCode and p.PatientOrder=prcpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.CPRIFSuccess='ROSC'and prcpr.CenterIFAuditForXFFS=1 )>0 ");
                    else if (m_CPRIFSuccess == "未审核")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p left join ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR prcpr on p.TaskCode=prcpr.TaskCode and p.PatientOrder=prcpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.MedicalStateCode!=0 and p.CPRIFSuccess='ROSC' and prcpr.CenterIFAuditForXFFS=0 )>0 ");
                    else if (m_CPRIFSuccess == "通过")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR p WHERE p.TaskCode=t.任务编码 and p.CenterAuditResult='" + m_CPRIFSuccess + "' )>0 ");
                    else if (m_CPRIFSuccess == "未通过")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR p WHERE p.TaskCode=t.任务编码 and p.CenterAuditResult='" + m_CPRIFSuccess + "' )>0 ");
                    else if (m_CPRIFSuccess == "待定")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR p WHERE p.TaskCode=t.任务编码 and p.CenterAuditResult='" + m_CPRIFSuccess + "' )>0 ");//20160328增加待定
                    else if (m_CPRIFSuccess == "已回访")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p LEFT JOIN ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR cpr on p.TaskCode=cpr.TaskCode and p.PatientOrder=cpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess='ROSC' and (cpr.DoctorFollowUp!='' or cpr.DoctorFollowUp!=null or DoctorFollowUpReason!='' or DoctorFollowUpReason!=null) )>0 ");
                    else if (m_CPRIFSuccess == "未回访")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p LEFT JOIN ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR cpr on p.TaskCode=cpr.TaskCode and p.PatientOrder=cpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess='ROSC' and (cpr.DoctorFollowUp='' or cpr.DoctorFollowUp=null or DoctorFollowUpReason='' or DoctorFollowUpReason=null) )>0 ");
                    else if (m_CPRIFSuccess == "出院")
                        sbWhereClause.Append(" AND (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p LEFT JOIN ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecordCPR cpr on p.TaskCode=cpr.TaskCode and p.PatientOrder=cpr.PatientOrder WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess='ROSC' and cpr.IFLeaveHospital='出院' )>0 ");
                    //else
                    //    sbWhereClause.Append(" WHERE (SELECT COUNT(*) FROM ").Append(builder.InitialCatalog).Append(".dbo.M_PatientRecord p WHERE p.TaskCode=t.任务编码 and p.CPRIFSuccess='" + m_CPRIFSuccess + "')>0 ");
                }
                if (searchBound == "4")
                {
                    if (loginInfo.WorkCode != "")
                        sbWhereClause.Append(" and (SELECT COUNT(*) FROM TTaskPersonLink ttpl WHERE t.任务编码=ttpl.任务编码 and ttpl.人员编码 = (select 编码 from TPerson where 工号='" + loginInfo.WorkCode + "'))>0 ");

                    else
                        sbWhereClause.Append(" and 1=2 ");
                }

                sbSQL.Append(sbWhereClause);

                sbSQL.Append(" select 任务编码1=t.任务编码,司机=dbo.GetNameWorkStr(t.任务编码,3),医生=dbo.GetNameWorkStr(t.任务编码,4),担架工=dbo.GetNameWorkStr(t.任务编码,6),护士=dbo.GetNameWorkStr(t.任务编码,5)");
                sbSQL.Append(@"into #tempY 
                        FROM TTask t 
                        JOIN TAlarmEvent ae ON t.事件编码=ae.事件编码 ");
                sbSQL.Append("where 1=1  ");//GetStr
                //sbSQL.Append("where t.任务编码>@TaskCodeB and t.任务编码<=@TaskCodeE  ");//20151217注释因为有挂起事件然后唤醒派车导致查询有误
                WhereClauseUtility.AddDateTimeGreaterThan("t.生成任务时刻", m_BeginTime, sbSQL);
                WhereClauseUtility.AddDateTimeLessThan("t.生成任务时刻", m_EndTime, sbSQL);
                WhereClauseUtility.AddStringEqual("ae.是否测试", isTest, sbSQL);//是否测试

                sbSQL.Append(" select identity(int,1,1) as 行号, * into #tempZ ");
                sbSQL.Append(" from #tempX t ");
                sbSQL.Append(" left join #tempY t1 on t.任务编码=t1.任务编码1 ");
                sbSQL.Append(" where 1=1 ");

                //if (m_IsFill == "True")
                //{
                //    sbSQL.Append(" AND 病历数>0");
                //}
                //else if (m_IsFill == "False")
                //{
                //    sbSQL.Append(" AND 病历数=0 ");
                //}
                switch (searchBound)//检索范围
                {
                    case "1"://全部
                        WhereClauseUtility.AddStringEqual("中心编码", m_CenterCode, sbSQL);
                        WhereClauseUtility.AddStringEqual("分站编码", m_StationCode, sbSQL);
                        WhereClauseUtility.AddStringLike("司机", m_Driver, sbSQL);
                        WhereClauseUtility.AddStringLike("医生", m_Doctor, sbSQL);
                        //WhereClauseUtility.AddStringLike("担架工", m_Litter, sbSQL);//20151216担架工修改为流水号
                        WhereClauseUtility.AddStringLike("护士", m_Nurse, sbSQL);
                        break;
                    case "2"://分中心
                        WhereClauseUtility.AddStringEqual("分站编码", m_StationCode, sbSQL);
                        WhereClauseUtility.AddStringEqual("中心编码", loginInfo.DispatchSubCenterID, sbSQL);
                        WhereClauseUtility.AddStringLike("司机", m_Driver, sbSQL);
                        WhereClauseUtility.AddStringLike("医生", m_Doctor, sbSQL);
                        //WhereClauseUtility.AddStringLike("担架工", m_Litter, sbSQL);
                        WhereClauseUtility.AddStringLike("护士", m_Nurse, sbSQL);
                        break;
                    case "3"://分站
                        WhereClauseUtility.AddStringEqual("中心编码", m_CenterCode, sbSQL);
                        WhereClauseUtility.AddStringEqual("分站编码", loginInfo.DispatchSationID, sbSQL);
                        WhereClauseUtility.AddStringLike("司机", m_Driver, sbSQL);
                        WhereClauseUtility.AddStringLike("医生", m_Doctor, sbSQL);
                        //WhereClauseUtility.AddStringLike("担架工", m_Litter, sbSQL);
                        WhereClauseUtility.AddStringLike("护士", m_Nurse, sbSQL);
                        break;
                    case "4"://本人
                        //WhereClauseUtility.AddStringEqual("中心编码", m_CenterCode, sbSQL);
                        //WhereClauseUtility.AddStringEqual("分站编码", m_StationCode, sbSQL);
                        break;
                }
                sbSQL.Append(@"ORDER BY 派车时刻 desc ");

                sbSQL.Append(@"select top " + rows + " A.*  from #tempZ A where 行号>" + (page - 1) * rows + " order by 行号 ");
                sbSQL.Append(@"SELECT count(*) FROM #tempZ t ");
                sbSQL.Append(" drop table #tempX,#tempY,#tempZ ");


                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sbSQL.ToString(), null);
                List<M_TaskPartInfo> list = new List<M_TaskPartInfo>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_TaskPartInfo info = new M_TaskPartInfo();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_TaskPartInfo();
                        info.TaskCode = dr["任务编码"].ToString();
                        info.PatientName = dr["患者姓名"].ToString();
                        //info.FirstAcceptTime = dr["首次受理时刻"].ToString();
                        info.FirstAcceptTime = dr["派车时刻"].ToString();//20150910修改为取生成任务时刻
                        info.ContactTelephone = dr["联系电话"].ToString();
                        info.EventType = dr["事件类型"].ToString();
                        info.SceneAddress = dr["现场地址"].ToString();
                        info.AcceptType = dr["受理类型"].ToString();
                        info.Station = dr["分站名称"].ToString();
                        info.ActualLogo = dr["实际标识"].ToString();
                        info.Driver = dr["司机"].ToString();
                        info.Doctor = dr["医生"].ToString();
                        info.Nurse = dr["护士"].ToString();
                        info.StretcherBearers = dr["担架工"].ToString();//20160106
                        info.OutCarResults = dr["出车结果"].ToString();
                        info.PatientNumber = dr["病历数"].ToString();
                        //info.ChargeNumber = dr["收费数"].ToString();
                        //info.IsSubmit = dr["是否提交"].ToString();
                        info.PatientNum = Convert.ToInt32(dr["患者人数"]); //add2021-10-15 plq 医保上线移植

                        list.Add(info);
                    }
                }
                else
                {
                    list = null;
                    var resultK = new { total = 0, rows = 0 };//当查询没有数据返回;
                    return resultK;
                }
                int total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                var result = new { total = total, rows = list };
                //return list;
                return result;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/GetTasks(" + m_BeginTime + " -- " + m_EndTime + ")", ex.ToString());
                return null;
            }
        }
        #endregion

        #region 出车信息-根据任务编码查询病历列表
        /// <summary>
        /// 根据任务编码查询病历列表
        /// </summary>
        /// <param name="taskCode"></param>
        /// <returns></returns>
        public List<M_PatientRecord> GetPatientCommonByTask(string taskCode)
        {
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(SqlHelper.AttemperConnectionString);//取调度库的链接字符串
                SqlConnectionStringBuilder builderyb = new SqlConnectionStringBuilder(SqlHelper.YBConnectionString); //add2021-10-19 plq 医保上线移植
                StringBuilder sbSQL = new StringBuilder();
                sbSQL.Append(" select TaskCode,PatientOrder,Name,Sex,Age=case when AgeType='不详' then Age else Age+AgeType end ");
                sbSQL.Append(" ,AgeType,LocalAddress,SendAddress,FirstAidEffect,DiseaseCooperation,FormCompleteLogo,UpdateFormCompleteTime ");
                sbSQL.Append(" ,SubmitLogo,SubmitTime,PatientVersion,MedicalState=case when MedicalStateCode=0 then '暂存' when MedicalStateCode=1 then '已提交' when MedicalStateCode=2 then '未通过' when MedicalStateCode=3 then '修改后提交' else '' end ");
                sbSQL.Append(",MedicalStateCode,IsCharge=isnull((SELECT IsCharge=case when cr.PatientOrder>0 then '已收' end FROM M_PatientCharge cr Where cr.TaskCode=pr.TaskCode and cr.PatientOrder=pr.PatientOrder),'未收')  ");
                sbSQL.Append(",Trace=isnull((SELECT COUNT(*) FROM M_PatientRecordTrace cr Where cr.TaskCode=pr.TaskCode and cr.PatientOrder=pr.PatientOrder),0)  ");
                sbSQL.Append(",IsYB=isnull((SELECT top 1 mpryb.操作状态 FROM ").Append(builderyb.InitialCatalog).Append(".dbo.M_PatientRecordYiBao mpryb Where mpryb.任务编码=pr.TaskCode and mpryb.病历序号=pr.PatientOrder and mpryb.操作状态 in (3,4) order by mpryb.交易时间 desc),0) ");//20210427 sr 判断收费医保交易 //add2021-10-19 plq 医保上线移植
                sbSQL.Append(",ta.车牌号码,startTime=tt.生成任务时刻,endTime=case when tt.完成时刻 is null then tt.返回站中时刻 when tt.返回站中时刻 is null and tt.完成时刻 is null then dateadd(hh,2,tt.生成任务时刻) else tt.完成时刻 end ");
                sbSQL.Append(" ,IFExecuting=tt.是否执行中");
                sbSQL.Append(" from M_PatientRecord pr");
                sbSQL.Append(" left join  ").Append(builder.InitialCatalog).Append(".dbo.TTask tt on pr.TaskCode=tt.任务编码 ");
                sbSQL.Append(" left join  ").Append(builder.InitialCatalog).Append(".dbo.TAmbulance ta on tt.车辆编码=ta.车辆编码 ");
                sbSQL.Append(" where pr.TaskCode='").Append(taskCode).Append("' ");
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sbSQL.ToString(), null);
                List<M_PatientRecord> list = new List<M_PatientRecord>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    M_PatientRecord info = new M_PatientRecord();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        info = new M_PatientRecord();
                        info.TaskCode = dr["TaskCode"].ToString();
                        info.PatientOrder = Convert.ToInt32(dr["PatientOrder"]);
                        info.Name = dr["Name"].ToString();
                        info.Sex = dr["Sex"].ToString();
                        info.Age = dr["Age"].ToString();
                        info.LocalAddress = dr["LocalAddress"].ToString();
                        info.SendAddress = dr["SendAddress"].ToString();
                        info.FirstAidEffect = dr["FirstAidEffect"].ToString();
                        info.DiseaseCooperation = dr["DiseaseCooperation"].ToString();
                        info.SubmitLogo = DBConvert.ConvertDBTypeToNullableBool(dr["SubmitLogo"]);
                        info.SubmitTime = DBConvert.ConvertDBTypeToNullable(dr["SubmitTime"]);
                        info.PatientVersion = dr["PatientVersion"].ToString();//事件类型
                        info.ForArea = dr["MedicalState"].ToString();//病历状态--借助区域字段
                        info.ForHelpTelephone = dr["IsCharge"].ToString();//是否收费--借助区域字段
                        info.Remark = dr["Trace"].ToString();//痕迹--借助“备注”字段
                        info.FormCompleteLogo = DBConvert.ConvertDBTypeToNullableBool(dr["FormCompleteLogo"]);//病历是否归档--20160119
                        info.UpdateFormCompletePerson = dr["UpdateFormCompleteTime"].ToString();//自动归档后，退回的操作时间--借助--20160119
                        info.DeathCertificate = dr["车牌号码"].ToString();//20160420-增加传给扁鹊
                        info.DiseaseCooperation = dr["startTime"].ToString();//20160420-增加传给扁鹊参数-开始时间
                        info.DiseaseNotCooperationSupplement = dr["endTime"].ToString();//20160420-增加传给扁鹊-结束时间
                        info.FormedConsent = DBConvert.ConvertDBTypeToNullableBool(dr["IFExecuting"]);//20160811-增加判断任务是否执行中
                        info.MedicalStateCode = Convert.ToInt32(dr["MedicalStateCode"]);//病历状态编码--20161129
                        info.RescueType = dr["IsYB"].ToString();//是否医保--借助“RescueType”字段 //add2021-10-19 plq 医保上线移植
                        list.Add(info);
                    }
                }
                else
                {
                    list = null;
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/GetPatientCommonByTask()", ex.ToString());
                return null;
            }
        }
        #endregion

        #endregion

        #region 根据病种分类的名称串来获取主诉和现病史
        /// <summary>
        /// 根据病种分类的名称串来获取主诉和现病史
        /// <param name="TemplateName">模版名称</param>
        /// <returns></returns>
        public object GetAllTemplate(string TemplateName)
        {
            List<string> AlarmReasons = new List<string>();
            List<string> HistoryOfPresentIllness = new List<string>();
            List<M_ZCaseTemplate> listmodel = new List<M_ZCaseTemplate>();
            string ret = "";//主诉串
            string xbs = "";//现病史串
            string[] names = TemplateName.Split(',');
            for (int i = 0; i < names.Length; i++)
            {
                string name = names[i].ToString();
                var list = from t in db.M_ZCaseTemplate
                           where t.Name == name
                           select new
                            {
                                ID = t.ID,
                                Name = t.Name,
                                AlarmReason = t.AlarmReason,
                                HistoryOfPresentIllness = t.HistoryOfPresentIllness,
                                SN = t.SN,
                                IsActive = t.IsActive
                            };
                //if (!string.IsNullOrEmpty(names[i]))
                //{
                //    list = list.Where(p => p.Name.Contains(names[i]));//like
                //}
                if (list.ToList().Count > 0)
                {
                    AlarmReasons.Add((list.FirstOrDefault().AlarmReason == "" || list.FirstOrDefault().AlarmReason == null) ? "" : list.FirstOrDefault().AlarmReason);//主诉
                    HistoryOfPresentIllness.Add((list.FirstOrDefault().HistoryOfPresentIllness == "" || list.FirstOrDefault().HistoryOfPresentIllness == null) ? "" : list.FirstOrDefault().HistoryOfPresentIllness);//现病史
                }
            }
            if (AlarmReasons.Count > 0)
                ret = AlarmReasons.Aggregate((commaText, next) => commaText + " " + next);
            if (HistoryOfPresentIllness.Count > 0)
                xbs = HistoryOfPresentIllness.Aggregate((commaText, next) => commaText + " " + next);

            listmodel.Add(new M_ZCaseTemplate
            {
                AlarmReason = ret,
                HistoryOfPresentIllness = xbs
            });
            var result = listmodel.ToList();
            return result;

        }
        #endregion

        #region 根据任务编码获取调度信息
        /// <summary>
        /// 根据任务编码获取调度信息
        /// </summary>
        /// <param name="taskCode"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public M_AttemperData GetAttemperData(string taskCode, string state)
        {
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(SqlHelper.AttemperConnectionString);//取调度库的链接字符串

                StringBuilder sbSQL = new StringBuilder();
                sbSQL.Append(@"SELECT 
                         TaskCode = tt.任务编码,Name = tac.患者姓名,Sex = tac.性别,Age = CONVERT(varchar(20),tac.年龄),
                         ForHelpPhone =tac.呼救电话,LinkPerson =tac.联系人,ContactTelephone = tac.联系电话,CallOrder=tt.用户流水号,
                         AlarmTypeCode =CONVERT(varchar(20),tae.事件类型编码),AlarmType = tzae.名称,Area = tae.区域,
                         XCoordinate = CONVERT(varchar(20),tac.X坐标),YCoordinate = CONVERT(varchar(20),tac.Y坐标),AlarmReason = tac.主诉,
                         LocalAddress = tac.现场地址,SendAddress = tac.送往地点,StationCode =tt.分站编码,Station =ts.名称,
                         AmbulanceCode =tt.车辆编码,FirstAcceptTime=tae.首次受理时刻,TelephoneRingingTime =tac.电话振铃时刻,GenerationTaskTime =tt.生成任务时刻,
                         DrivingTime =tt.出车时刻,ArriveSceneTime =tt.到达现场时刻,LeaveSceneTime=tt.离开现场时刻,ArriveDestinationTime =tt.到达医院时刻,
                         CompleteTime =tt.完成时刻,BackTime =tt.返回站中时刻,Disptcher = tp.姓名,
                         OutResult = case when tt.是否正常结束 = 1 then '正常' else '异常结束' end,");
                sbSQL.Append("Driver = ").Append(builder.InitialCatalog).Append(".dbo.GetStr(tt.任务编码,3),");
                sbSQL.Append("Doctor = ").Append(builder.InitialCatalog).Append(".dbo.GetStr(tt.任务编码,4),");
                sbSQL.Append("Nurse = ").Append(builder.InitialCatalog).Append(".dbo.GetStr(tt.任务编码,5),");
                sbSQL.Append("StretcherBearers = ").Append(builder.InitialCatalog).Append(".dbo.GetStr(tt.任务编码,6),");
                sbSQL.Append("FirstAider = ").Append(builder.InitialCatalog).Append(".dbo.GetStr(tt.任务编码,7) ");

                sbSQL.Append("FROM ").Append(builder.InitialCatalog).Append(".dbo.TTask tt ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TAcceptEvent tac on tt.事件编码=tac.事件编码 AND tt.受理序号=tac.受理序号  ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TAlarmEvent tae on tt.事件编码=tae.事件编码 ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TPerson tp on tt.责任调度人编码=tp.编码  ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TStation ts on tt.分站编码=ts.编码  ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TZTaskAbendReason tztar on tt.异常结束原因编码=tztar.编码 ");
                sbSQL.Append("LEFT JOIN  ").Append(builder.InitialCatalog).Append(".dbo.TZAlarmEventType tzae ON tae.事件类型编码 = tzae.编码 ");
                sbSQL.Append("where tt.任务编码='" + taskCode + "' ");

                M_AttemperData result = db.Database.SqlQuery<M_AttemperData>(sbSQL.ToString()).FirstOrDefault();

                return result;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/GetAttemperData()", ex.ToString());
                return null;
            }
        }
        #endregion

        #region 获取病历序号最大值
        public int GetPatientMaxOrder(string taskCode)
        {
            M_PatientRecord pInfo = new M_PatientRecord();
            int PatientOrder = 0;
            var query = from pr in db.M_PatientRecord
                        where pr.TaskCode == taskCode
                        select pr.PatientOrder;
            if (query.Count() > 0)
                PatientOrder = query.Max() + 1;
            else
                PatientOrder = 1;
            return PatientOrder;
        }
        #endregion

        #endregion

        #region 获取病历主表及子表信息

        #region 获取病历主表及附表信息
        /// <summary>
        /// 获取病历主表及附表信息
        /// </summary>
        /// <param name="taskCode"></param>
        /// <param name="patientOrder"></param>
        /// <param name="info"></param>
        /// <param name="pra"></param>
        /// <param name="prCPR"></param>
        public void GetPatientInfo(string taskCode, int patientOrder, out object info
            , out M_PatientRecordAppend pra, out M_PatientRecordCPR prCPR)
        {
            info = (from pi in db.M_PatientRecord
                    where pi.TaskCode == taskCode && pi.PatientOrder == patientOrder
                    select pi).FirstOrDefault();//病历主表

            pra = (from pi in db.M_PatientRecordAppend
                   where pi.TaskCode == taskCode && pi.PatientOrder == patientOrder
                   select pi).FirstOrDefault();//病历附表--体检等信息

            prCPR = (from pi in db.M_PatientRecordCPR
                     where pi.TaskCode == taskCode && pi.PatientOrder == patientOrder
                     select pi).FirstOrDefault();//心肺复苏
        }
        #endregion

        #region 获取病历救治记录列表
        public List<M_PatientRecordRescue> GetPRRescueList(int page, int rows, ref int rowCounts, string taskCode, int patientOrder)
        {
            var q = from ps in db.M_PatientRecordRescue
                    where ps.TaskCode == taskCode && ps.PatientOrder == patientOrder
                    select ps;

            rowCounts = q.Count();
            var t = q.OrderBy(u => u.PeriodOfTimeCode).ThenBy(u => u.DisposeOrder).Skip((page - 1) * rows).Take(rows).ToList();
            return t;
        }
        #endregion

        #region 获取病历救治记录主表
        public void GetPRRescueInfo(string taskCode, int patientOrder, string rescueRecordCode, int disposeOrder
            , out M_PatientRecordRescue prrInfo)
        {
            prrInfo = (from pi in db.M_PatientRecordRescue
                       where pi.TaskCode == taskCode && pi.PatientOrder == patientOrder && pi.RescueRecordCode == rescueRecordCode && pi.DisposeOrder == disposeOrder
                       select pi).FirstOrDefault();//救治记录主表

        }
        #endregion


        #region 获取救治记录序号最大值--暂时没用
        public string GetRescueRecordMaxCode(string taskCode, int patientOrder)
        {
            M_PatientRecordRescue pInfo = new M_PatientRecordRescue();
            string RescueRecordCode = "";
            var query = from pr in db.M_PatientRecordRescue
                        where pr.TaskCode == taskCode && pr.PatientOrder == patientOrder
                        select pr.RescueRecordCode;
            if (query.Count() > 0)
                RescueRecordCode = taskCode + (query.Count() + 1);
            else
                RescueRecordCode = taskCode + "1";
            return RescueRecordCode;
        }
        #endregion

        #endregion


        #region 新增

        #region 新增病历主表和附表
        public bool Insert(object info, M_PatientRecordAppend pra
            , M_PatientRecordCPR prCPR, List<M_PatientRecordDiag> prDiag
            , List<M_PatientRecordECGImpressions> prECG)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                string IMEI = "";
                try
                {
                    M_PatientRecord pInfo = (M_PatientRecord)info;

                    //20161121--新增病历取序号
                    M_PatientRecordDAL prDAL = new M_PatientRecordDAL();
                    int AddPatientOrder = prDAL.GetPatientMaxOrder(pInfo.TaskCode);

                    pInfo.PatientOrder = AddPatientOrder;//20161121--序号
                    pInfo.MedicalRecordGenerationTime = DateTime.Now;//病历生成时间
                    db.M_PatientRecord.Add(pInfo);//插入病历主表
                    IMEI = pInfo.IMEI;//20150827判断是PC填写还是PAD填写

                    //同时更新受理表中20161123
                    if (AppConfig.GetConfigBool("IsUpdatePName"))
                    {
                        string eventCode = pInfo.TaskCode.Substring(0, 16);//取事件编码
                        int accOrder = Convert.ToInt32(pInfo.TaskCode.Substring(16, 2));//取受理序号
                        AcceptEventInfo originAccInfo = new AcceptEventInfo();
                        originAccInfo.EventCode = eventCode;
                        originAccInfo.AcceptOrder = accOrder;
                        UpdateAcceptEvent(originAccInfo, pInfo,pra);//修改配置更新受理表
                    }

                    if (pra != null)
                    {
                        pra.PatientOrder = AddPatientOrder;//20161121--序号
                        db.M_PatientRecordAppend.Add(pra);//插入病历附表--体检等
                    }
                    if (prCPR != null)
                    {
                        //prCPR.RegistrationTime = DateTime.Now;//登记时间
                        prCPR.PatientOrder = AddPatientOrder;//20161121--序号
                        db.M_PatientRecordCPR.Add(prCPR);//插入病历附表--心肺复苏
                    }
                    if (prDiag != null)
                    {
                        db.M_PatientRecordDiag.AddRange(prDiag);//插入初步诊断子表
                    }
                    if (prECG != null)
                    {
                        db.M_PatientRecordECGImpressions.AddRange(prECG);//插入心电图印象子表
                    }

                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    //transaction.Complete();//这一句如果注释后，则上面的保存不会被提交到DB中
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/Insert(" + IMEI + ")", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #region 新增救治记录
        //新增救治记录
        public bool InsertPRRescue(M_PatientRecordRescue info, List<M_PatientRecordMeasure> measureSCO
            , List<M_PatientRecordDrug> drugSCO, List<M_PatientRecordSanitation> sanitationSCO
            , List<M_PatientRecordLossDrug> lossDrugSCO, List<M_PatientRecordLossSanitation> lossSanitationSCO)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    string RescueRecordCode = "";
                    //M_PatientRecordRescue q = (from pr in db.M_PatientRecordRescue
                    //                           orderby (pr.RescueRecordCode.Substring(21, pr.RescueRecordCode.Length)) descending
                    //                           where pr.TaskCode == info.TaskCode && pr.PatientOrder == info.PatientOrder
                    //                           select pr).FirstOrDefault();
                   List<M_PatientRecordRescue> q = (from pr in db.M_PatientRecordRescue
                                               //orderby pr.RescueRecordCode descending
                                               where pr.TaskCode == info.TaskCode && pr.PatientOrder == info.PatientOrder
                                               select pr).ToList();
                    if (q != null)
                    {
                        RescueRecordCode = info.TaskCode + (q.Count + 1).ToString();//20160922

                    }
                        //RescueRecordCode = info.TaskCode + (Convert.ToInt32(q.RescueRecordCode.Replace(info.TaskCode, "")) + 1).ToString();
                    //RescueRecordCode = info.TaskCode + (query.Count() + 1);
                    else
                        RescueRecordCode = info.TaskCode + "1";
                    info.RescueRecordCode = RescueRecordCode;//救治记录编码
                    db.M_PatientRecordRescue.Add(info);//插入救治记录

                    if (info != null)
                    {
                        if (measureSCO != null)
                        {
                            List<M_PatientRecordMeasure> measure = new List<M_PatientRecordMeasure>();
                            for (int k = 0; k < measureSCO.Count; k++)
                            {
                                M_PatientRecordMeasure SInfo = measureSCO[k];
                                //M_PatientRecordMeasure MeasureInfo = measureSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.RescueMeasureCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;//救治记录编码
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.RescueMeasureCode = c.ID;//救治措施编码
                                SInfo.RescueMeasureName = c.Name;//救治措施
                                SInfo.OtherTypeID = c.OtherTypeID;//
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;//单价
                                int FeeScale = c.FeeScale == null ? 1 : Convert.ToInt32(c.FeeScale);
                                int Dosage = 1;
                                if (FeeScale == -1)
                                {
                                    Dosage = 1;
                                }
                                else if (FeeScale == 1)
                                {
                                    Dosage = Convert.ToInt32(SInfo.NumberOfTimes);
                                }
                                else
                                {
                                    if (FeeScale < 0 && (-FeeScale <= Convert.ToInt32(SInfo.NumberOfTimes)))
                                    {
                                        Dosage = -FeeScale;
                                    }
                                    else
                                    {
                                        Dosage = Convert.ToInt32(SInfo.NumberOfTimes);
                                    }
                                }

                                SInfo.TotalPrice = c.RealPrice * Dosage;//合计金额
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.Remark = SInfo.Remark;
                                SInfo.SelectOrder = k + 1;
                                measure.Add(SInfo);
                                //measureSCO.Add(SInfo);
                                //db.M_PatientRecordMeasure.Add(SInfo);//插入救治措施子表
                            }
                            db.M_PatientRecordMeasure.AddRange(measure);//插入救治措施子表
                        }
                        if (drugSCO != null)
                        {
                            List<M_PatientRecordDrug> drug = new List<M_PatientRecordDrug>();
                            for (int l = 0; l < drugSCO.Count; l++)
                            {
                                M_PatientRecordDrug DInfo = drugSCO[l];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == DInfo.DrugCode
                                                select m).FirstOrDefault();
                                DInfo.TaskCode = DInfo.TaskCode;
                                DInfo.PatientOrder = DInfo.PatientOrder;
                                DInfo.RescueRecordCode = RescueRecordCode;
                                DInfo.DisposeOrder = DInfo.DisposeOrder;//处理序号
                                DInfo.DrugCode = c.ID;
                                DInfo.DrugName = c.Name;
                                DInfo.GiveMedicineWay = DInfo.GiveMedicineWay;
                                string ChargeWay = "";
                                string RealPrice = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero).ToString();
                                if (c.FeeScale > 1)
                                { ChargeWay = RealPrice + "元/" + "1-" + c.FeeScale + c.TDictionary2.Name; }
                                else
                                { ChargeWay = RealPrice + "元/" + c.TDictionary2.Name; }
                                DInfo.ChargeWay = ChargeWay;//收费方式
                                DInfo.Specifications = c.Specification;//规格
                                DInfo.Unit = c.TDictionary2.Name;//单位
                                DInfo.Dosage = DInfo.Dosage;//用量
                                DInfo.Price = c.RealPrice;//单价
                                int FeeScale = c.FeeScale == null ? 1 : Convert.ToInt32(c.FeeScale);
                                int Dosage = 1;//Convert.ToInt32(DInfo.Dosage)
                                double dosage = Math.Ceiling(Convert.ToDouble(DInfo.Dosage));
                                if (Convert.ToInt32(dosage) % FeeScale > 0)
                                {
                                    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToInt32(dosage) / FeeScale), 0)) + 1;
                                }
                                else
                                {
                                    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(Convert.ToInt32(dosage) / FeeScale), 0));
                                }
                                //20151106增加如果“最大收费刻度”不为空
                                if (c.LimitMaxPrice != null)
                                {
                                    //计算价格大于最大收费刻度,取最大收费刻度，否则取计算价格
                                    if ((c.RealPrice * Dosage) > c.LimitMaxPrice)
                                    {
                                        DInfo.TotalPrice = c.LimitMaxPrice;
                                    }
                                    else
                                    {
                                        DInfo.TotalPrice = c.RealPrice * Dosage;
                                    }
                                }
                                //DInfo.TotalPrice = c.RealPrice * Dosage;//合计金额
                                DInfo.TotalDose = DInfo.TotalDose;//合计用量
                                DInfo.Remark = DInfo.Remark;//备注
                                DInfo.SelectOrder = l + 1;//选择序号
                                DInfo.FeeScale = c.FeeScale;
                                drug.Add(DInfo);
                                //db.M_PatientRecordDrug.Add(DInfo);//插入药品子表
                            }
                            db.M_PatientRecordDrug.AddRange(drug);//插入药品子表
                        }
                        if (sanitationSCO != null)
                        {
                            List<M_PatientRecordSanitation> sanitation = new List<M_PatientRecordSanitation>();
                            for (int k = 0; k < sanitationSCO.Count; k++)
                            {
                                M_PatientRecordSanitation SInfo = sanitationSCO[k];
                                //M_PatientRecordSanitation SInfo = null;
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.SanitationCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.SanitationCode = c.ID;
                                SInfo.SanitationName = c.Name;
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;
                                SInfo.TotalPrice = c.RealPrice * SInfo.NumberOfTimes;//合计金额
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.SelectOrder = k + 1;
                                sanitation.Add(SInfo);
                            }
                            db.M_PatientRecordSanitation.AddRange(sanitation);//插入耗材子表
                        }
                        if (lossDrugSCO != null)
                        {
                            List<M_PatientRecordLossDrug> lossDrug = new List<M_PatientRecordLossDrug>();
                            for (int k = 0; k < lossDrugSCO.Count; k++)
                            {
                                M_PatientRecordLossDrug SInfo = lossDrugSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.DrugCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;
                                SInfo.DisposeOrder = SInfo.DisposeOrder;
                                SInfo.DrugCode = c.ID;
                                SInfo.DrugName = c.Name;//药品名称
                                SInfo.GiveMedicineWay = SInfo.GiveMedicineWay;//应该是空
                                string ChargeWay = "";
                                string RealPrice = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero).ToString();
                                if (c.FeeScale > 1)
                                { ChargeWay = RealPrice + "元/" + "1-" + c.FeeScale + c.TDictionary2.Name; }
                                else
                                { ChargeWay = RealPrice + "元/" + c.TDictionary2.Name; }
                                SInfo.ChargeWay = ChargeWay;//收费方式
                                SInfo.Specifications = c.Specification;//规格
                                SInfo.Unit = c.TDictionary2.Name;//单位
                                SInfo.Dosage = SInfo.Dosage;//用量
                                SInfo.Price = c.RealPrice;//单价
                                SInfo.TotalPrice = c.RealPrice * SInfo.Dosage;//合计金额
                                SInfo.TotalDose = SInfo.TotalDose;//合计用量
                                SInfo.Remark = SInfo.Remark;//备注
                                SInfo.SelectOrder = k + 1;
                                lossDrug.Add(SInfo);
                            }
                            db.M_PatientRecordLossDrug.AddRange(lossDrug);//插入损耗药品子表
                        }
                        if (lossSanitationSCO != null)
                        {
                            List<M_PatientRecordLossSanitation> lossSanitation = new List<M_PatientRecordLossSanitation>();
                            for (int k = 0; k < lossSanitationSCO.Count; k++)
                            {
                                M_PatientRecordLossSanitation SInfo = lossSanitationSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.SanitationCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;//救治记录编码
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.SanitationCode = c.ID;
                                SInfo.SanitationName = c.Name;
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.TotalPrice = c.RealPrice * SInfo.NumberOfTimes;//合计金额
                                SInfo.Remark = SInfo.Remark;
                                SInfo.SelectOrder = k + 1;
                                lossSanitation.Add(SInfo);
                            }
                            db.M_PatientRecordLossSanitation.AddRange(lossSanitation);//插入损耗卫生材料子表
                        }
                    }

                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    //transaction.Complete();//这一句如果注释后，则上面的保存不会被提交到DB中
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/InsertPRRescue()", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #endregion
        
        #region 更新受理表患者姓名20161123
        /// <summary>
        /// 20161216--增加M_PatientRecordAppend表
        /// </summary>
        /// <param name="accinfo"></param>
        /// <param name="mpr"></param>
        /// <param name="pra"></param>
        /// <returns></returns>
        public bool UpdateAcceptEvent(AcceptEventInfo accinfo, M_PatientRecord mpr,M_PatientRecordAppend pra)
        {
                try
                {
                    StringBuilder strSQL = new StringBuilder();
                    strSQL.Append(@"update tac set tac.患者姓名='" + mpr.Name + "' ");
                    if (AppConfig.GetConfigBool("IsSex") && mpr.Sex != "")
                        strSQL.Append(@" ,tac.性别='" + mpr.Sex + "' ");
                    if (AppConfig.GetConfigBool("IsAge") && mpr.Age != "")
                        strSQL.Append(@" ,tac.年龄='" + mpr.Age + mpr.AgeType + "' ");
                    if (AppConfig.GetConfigBool("IsAlarmReason") && mpr.AlarmReason != "")
                        strSQL.Append(@" ,tac.主诉='" + mpr.AlarmReason + "' ");
                    if (AppConfig.GetConfigBool("IsLocalAddr") && mpr.LocalAddress != "")
                        strSQL.Append(@" ,tac.现场地址='" + mpr.LocalAddress + "' ");
                    if (AppConfig.GetConfigBool("IsWaitAddr") && mpr.LocalAddress != "")
                        strSQL.Append(@" ,tac.等车地址='" + mpr.LocalAddress + "' ");
                    if (AppConfig.GetConfigBool("IsSendAddr") && mpr.SendAddress != "")
                        strSQL.Append(@" ,tac.送往地点='" + mpr.SendAddress + "' ");
                    //20161210--增加判断初步判断--20161221增加初步判断补充
                    if (AppConfig.GetConfigBool("IsJudge") && pra.FirstImpression != "")
                    {
                        string FirstImpression="";
                        if (pra.FirstImpressionSupplement != "")
                            FirstImpression = pra.FirstImpression.Substring(0, pra.FirstImpression.Length - 1) + "(" + pra.FirstImpressionSupplement + ")";
                        else
                            FirstImpression = pra.FirstImpression.Substring(0, pra.FirstImpression.Length - 1);
                        strSQL.Append(@" ,tac.病种判断='" + FirstImpression + "' ");
                    }


                    //20161205--去掉中止任务和事件类型为保障的，不更新
                    strSQL.Append(@" from TAcceptEvent tac,[TAlarmEvent] tae,[TTask] tt ");
                    strSQL.Append(@" where tac.事件编码=tae.事件编码 and tac.事件编码=tt.事件编码 and tac.受理序号=tt.受理序号 ");
                    
                    //20161209--增加判断是否去除中止任务的
                    if (AppConfig.GetConfigBool("IsRemoveTaskStop"))
                        strSQL.Append(@" and tt.是否正常结束=1 ");
                    //20161209--增加判断是否去除事件类型为保障的
                    if (AppConfig.GetConfigBool("IsRemoveGuarantee"))
                        strSQL.Append(@" and tae.事件类型编码<>105 "); //2017-07-06 周志猛 将“事故类型编码”改为“事件类型编码”

                    strSQL.Append(@" and tac.事件编码='" + accinfo.EventCode + "' and tac.受理序号=" + accinfo.AcceptOrder + "  ");
                    

                    int ds = SqlHelper.ExecuteNonQuery(SqlHelper.AttemperConnectionString, CommandType.Text, strSQL.ToString(), null);
                    return ds > 0 ? true : false;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/UpdateAcceptEvent(PC)", ex.ToString());
                    return false;
                }
            
        }
        #endregion

        #region 修改病历主表和附表
        public bool Update(object info, M_PatientRecordAppend pra
            , M_PatientRecordCPR prCPR, List<M_PatientRecordDiag> prDiag
            , List<M_PatientRecordECGImpressions> prECG, string superRole)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    #region 修改病历主表

                    M_PatientRecord pInfo = (M_PatientRecord)info;
                    List<M_PatientRecordTrace> crackList = new List<M_PatientRecordTrace>();//病历修改记录
                    int j = 0;//病历修改记录主键
                    DateTime modifyTime = DateTime.Now;//修改时间
                    string modifyPerson = pInfo.LastUpdatePerson;//修改人
                    int order = -1;//20161123

                    M_PatientRecord originInfo = dalContext.IM_PatientRecordDAL.GetModelWithOutTrace(a => a.TaskCode == pInfo.TaskCode && a.PatientOrder == pInfo.PatientOrder);

                    var entry = db.Entry(pInfo);
                    if (entry.State == EntityState.Detached)
                    {

                        //如果已经被上下文追踪
                        if (originInfo != null)
                        {
                            //20161123
                            int maxOrder = 0;
                            order = pInfo.PatientOrder;//病历序号
                            var query = from pr in db.M_PatientRecord
                                        where pr.TaskCode == pInfo.TaskCode
                                        select pr.PatientOrder;
                            if (query.Count() > 0)
                                maxOrder = query.Max();
                            else
                                maxOrder = 0;
                            //如果是最后一个病历，才同时更新受理表中20161123
                            if (order == maxOrder && AppConfig.GetConfigBool("IsUpdatePName"))
                            {
                                string eventCode = pInfo.TaskCode.Substring(0, 16);//取事件编码
                                int accOrder = Convert.ToInt32(pInfo.TaskCode.Substring(16, 2));//取受理序号
                                AcceptEventInfo originAccInfo = new AcceptEventInfo();
                                originAccInfo.EventCode = eventCode;
                                originAccInfo.AcceptOrder = accOrder;
                                UpdateAcceptEvent(originAccInfo, pInfo, pra);//修改配置更新受理表
                            }
                            //-------end--------

                            //20161129--增加病历状态为未通过的也修改
                            if ((originInfo.SubmitLogo == true && (originInfo.MedicalStateCode==2 || superRole == "SuperRole")) || originInfo.SubmitLogo == false)
                            {
                                //originInfo = pInfo;//修改病历主表
                                pInfo.ForHelpTelephone = originInfo.ForHelpTelephone;
                                pInfo.OriginalTaskType = originInfo.OriginalTaskType;//原事件类型--不修改
                                pInfo.ForArea = originInfo.ForArea;
                                pInfo.AgentCode = originInfo.AgentCode;
                                pInfo.AgentWorkID = originInfo.AgentWorkID;//填写人工号
                                pInfo.AgentName = originInfo.AgentName;//填写人姓名
                                pInfo.BeginFillPatientTime = originInfo.BeginFillPatientTime;//开始填写病历时间
                                pInfo.MedicalRecordGenerationTime = originInfo.MedicalRecordGenerationTime;//病历生成时间
                                if (originInfo.FormCompleteLogo == true)
                                {
                                    pInfo.FormCompleteLogo = originInfo.FormCompleteLogo;//归档标识
                                    pInfo.FormCompleteTime = originInfo.FormCompleteTime;//归档时间
                                }
                                pInfo.ChargeOrder = originInfo.ChargeOrder;
                                if (originInfo.SubmitLogo == true)
                                {
                                    pInfo.SubmitLogo = originInfo.SubmitLogo;//提交标志
                                    pInfo.SubmitTime = originInfo.SubmitTime;//提交时间
                                }
                                //20161129--增加如果已经提交病历的病历状态不为“未通过”
                                if (originInfo.SubmitLogo == true && originInfo.MedicalStateCode != 2)
                                {
                                    pInfo.MedicalStateCode = originInfo.MedicalStateCode;//不修改病历状态
                                }
                                pInfo.SubCenterIFSpotChecks = originInfo.SubCenterIFSpotChecks;//分中心是否抽查
                                pInfo.SubCenterPerson = originInfo.SubCenterPerson;
                                pInfo.SubCenterSpotChecksTime = originInfo.SubCenterSpotChecksTime;
                                pInfo.SubCenterSpotChecksResult = originInfo.SubCenterSpotChecksResult;
                                pInfo.SubCenterSpotChecksUnqualifiedReason = originInfo.SubCenterSpotChecksUnqualifiedReason;
                                pInfo.SubCenterSpotChecksRmark = originInfo.SubCenterSpotChecksRmark;
                                pInfo.CenterIFSpotChecks = originInfo.CenterIFSpotChecks;//中心是否抽查
                                pInfo.CenterSpotChecksPerson = originInfo.CenterSpotChecksPerson;
                                pInfo.CenterSpotChecksTime = originInfo.CenterSpotChecksTime;
                                pInfo.CenterSpotChecksResult = originInfo.CenterSpotChecksResult;
                                pInfo.CenterSpotChecksRmark = originInfo.CenterSpotChecksRmark;

                                pInfo.CenterSpotChecksUnqualifiedReason = originInfo.CenterSpotChecksUnqualifiedReason;
                                pInfo.IFNoticeAttendNursingLectures = originInfo.IFNoticeAttendNursingLectures;
                                pInfo.NoticeAttendTime = originInfo.NoticeAttendTime;
                                pInfo.NoticeAttendPerson = originInfo.NoticeAttendPerson;
                                pInfo.ActualAttendNursingLectures = originInfo.ActualAttendNursingLectures;
                                pInfo.ActualAttendTime = originInfo.ActualAttendTime;
                                pInfo.ActualAttendPerson = originInfo.ActualAttendPerson;
                                pInfo.IFFillNoticeAttendNursingLectures = originInfo.IFFillNoticeAttendNursingLectures;
                                pInfo.FillNoticeAttendTime = originInfo.FillNoticeAttendTime;
                                pInfo.FillNoticeAttendPerson = originInfo.FillNoticeAttendPerson;
                                pInfo.FillActualAttendNursingLectures = originInfo.FillActualAttendNursingLectures;
                                pInfo.FillActualAttendTime = originInfo.FillActualAttendTime;
                                pInfo.FillActualAttendPerson = originInfo.FillActualAttendPerson;
                                pInfo.IFMedicalHistoryWritingProgress = originInfo.IFMedicalHistoryWritingProgress;
                                pInfo.IFSubCenterMedicalHistoryWritingProgress = originInfo.IFSubCenterMedicalHistoryWritingProgress;
                                pInfo.IFFill2NoticeAttendNursingLectures = originInfo.IFFill2NoticeAttendNursingLectures;
                                pInfo.Fill2NoticeAttendTime = originInfo.Fill2NoticeAttendTime;
                                pInfo.Fill2NoticeAttendPerson = originInfo.Fill2NoticeAttendPerson;
                                pInfo.Fill2ActualAttendNursingLectures = originInfo.Fill2ActualAttendNursingLectures;
                                pInfo.Fill2ActualAttendTime = originInfo.Fill2ActualAttendTime;
                                pInfo.Fill2ActualAttendPerson = originInfo.Fill2ActualAttendPerson;

                                pInfo.CenterFollowUp = originInfo.CenterFollowUp;
                                pInfo.CenterFollowUpSupplement = originInfo.CenterFollowUpSupplement;
                                pInfo.UpdateFormCompletePerson = originInfo.UpdateFormCompletePerson;
                                pInfo.UpdateFormCompleteTime = originInfo.UpdateFormCompleteTime;

                                if (originInfo.IMEI == "PAD")
                                    pInfo.IMEI = originInfo.IMEI;//20160425--增加不更改PAD填写的IMEI

                                db.M_PatientRecord.Attach(pInfo);//将给定实体附加到集的基础上下文中，将实体以“未更改”的状态放置到上下文；
                                db.Entry(pInfo).State = EntityState.Modified;
                            }

                        }
                        //else//如果不在当前上下文追踪
                        //{
                        //    db.Entry(pInfo).State = EntityState.Modified;
                        //}

                    }
                    #endregion

                    #region 修改病历附表--体检等
                    M_PatientRecordAppend praInfo = dalContext.IM_PatientRecordAppendDAL.GetModelWithOutTrace(a => a.TaskCode == pInfo.TaskCode && a.PatientOrder == pInfo.PatientOrder);
                    if (praInfo != null)
                    {
                        //20161129--增加病历状态为未通过的也修改
                        if ((originInfo.SubmitLogo == true && (originInfo.MedicalStateCode == 2 || superRole == "SuperRole")) || originInfo.SubmitLogo == false)
                        {
                            //pra.PhysicalExaminationSupplement = praInfo.PhysicalExaminationSupplement;//20150916放开体检补充描述
                            db.M_PatientRecordAppend.Attach(pra);//将给定实体附加到集的基础上下文中，将实体以“未更改”的状态放置到上下文；
                            db.Entry(pra).State = EntityState.Modified;//修改病历附表--体检等
                        }
                    }
                    else if (praInfo == null && pra != null)
                    {
                        db.M_PatientRecordAppend.Add(pra);//插入病历附表--体检等
                    }
                    #endregion

                    #region 修改病历附表--心肺复苏
                    M_PatientRecordCPR prCPRInfo = dalContext.IM_PatientRecordCPRDAL.GetModelWithOutTrace(a => a.TaskCode == pInfo.TaskCode && a.PatientOrder == pInfo.PatientOrder);
                    if (prCPRInfo != null && prCPR != null)
                    {
                        //20161129--增加病历状态为未通过的也修改
                        if ((originInfo.SubmitLogo == true && (originInfo.MedicalStateCode == 2 || superRole == "SuperRole")) || originInfo.SubmitLogo == false)
                        {
                            prCPR.EmergencyTime = prCPRInfo.EmergencyTime;//急救时长(页面没有显示，最后删除)
                            prCPR.ECGMonitoringTime = prCPRInfo.ECGMonitoringTime;//心电监护时间(页面没有显示，最后删除)
                            //prCPR.BeforeResuscitationECGDiagnosis = prCPRInfo.BeforeResuscitationECGDiagnosis;//20150916放开复苏前心电图诊断
                            prCPR.BeforeResuscitationSaO2 = prCPRInfo.BeforeResuscitationSaO2;
                            prCPR.IFLeaveHospital = prCPRInfo.IFLeaveHospital;//中心审核人员填写
                            prCPR.LeaveHospitalDate = prCPRInfo.LeaveHospitalDate;
                            prCPR.LeaveHospitalCPC = prCPRInfo.LeaveHospitalCPC;
                            prCPR.VerifyResults = prCPRInfo.VerifyResults;
                            prCPR.VerifyPerson = prCPRInfo.VerifyPerson;
                            prCPR.VerifyTime = prCPRInfo.VerifyTime;
                            prCPR.RegistrationPerson = prCPRInfo.RegistrationPerson;
                            prCPR.RegistrationTime = prCPRInfo.RegistrationTime;
                            prCPR.CenterIFAuditForXFFS = prCPRInfo.CenterIFAuditForXFFS;//中心审核心肺复苏
                            prCPR.CenterAuditPerson = prCPRInfo.CenterAuditPerson;
                            prCPR.CenterAuditTime = prCPRInfo.CenterAuditTime;
                            prCPR.CenterAuditResult = prCPRInfo.CenterAuditResult;
                            prCPR.CenterNotThroughReason = prCPRInfo.CenterNotThroughReason;
                            db.M_PatientRecordCPR.Attach(prCPR);//将给定实体附加到集的基础上下文中，将实体以“未更改”的状态放置到上下文；
                            db.Entry(prCPR).State = EntityState.Modified;//修改病历附表--心肺复苏
                        }
                    }
                    else if (prCPRInfo == null && prCPR != null)
                    {
                        db.M_PatientRecordCPR.Add(prCPR);//插入病历附表--心肺复苏
                    }
                    #endregion

                    #region 修改初步诊断子表
                    //20161129--增加病历状态为未通过的也修改
                    if ((originInfo.SubmitLogo == true && (originInfo.MedicalStateCode == 2 || superRole == "SuperRole")) || originInfo.SubmitLogo == false)
                    {
                        var dqry0 = from infos in db.M_PatientRecordDiag
                                    where infos.TaskCode == pInfo.TaskCode && infos.PatientOrder == pInfo.PatientOrder
                                    select infos;
                        foreach (var inf in dqry0)
                            db.M_PatientRecordDiag.Remove(inf);//删除初步诊断子表
                        //db.M_PatientRecordDiag.RemoveRange(prDiag);//删除初步诊断子表
                        if (prDiag != null)
                        {
                            db.M_PatientRecordDiag.AddRange(prDiag);//插入初步诊断子表
                        }
                    }
                    #endregion

                    #region 修改心电图印象子表
                    //20161129--增加病历状态为未通过的也修改
                    if ((originInfo.SubmitLogo == true && (originInfo.MedicalStateCode == 2 || superRole == "SuperRole")) || originInfo.SubmitLogo == false)
                    {
                        var dqry1 = from infos in db.M_PatientRecordECGImpressions
                                    where infos.TaskCode == pInfo.TaskCode && infos.PatientOrder == pInfo.PatientOrder
                                    select infos;
                        foreach (var inf in dqry1)
                            db.M_PatientRecordECGImpressions.Remove(inf);//删除心电图印象
                        //db.M_PatientRecordECGImpressions.RemoveRange(prECG);//删除心电图印象
                        if (prECG != null)
                        {
                            db.M_PatientRecordECGImpressions.AddRange(prECG);//插入心电图印象子表
                        }
                    }
                    #endregion

                    #region 提交病历后修改病历痕迹
                    int flag = 0;//病历状态：0.普通保存(还没有提交)；1.提交；2.未通过；3.修改后提交
                    //提交后保存的
                    if (originInfo.MedicalStateCode > 0)
                    {
                        flag = 2;//提交后的保存
                        #region 插入病历主表痕迹
                        //获得所有property的信息
                        PropertyInfo[] proOrigin = originInfo.GetType().GetProperties();
                        //PropertyInfo[] proNew = pInfo.GetType().GetProperties();
                        for (int i = 0; i < proOrigin.Length; i++)
                        {
                            object obj1 = proOrigin[i].GetValue(originInfo, null);
                            object obj2 = proOrigin[i].GetValue(info, null);
                            obj1 = obj1 == null ? "" : obj1;
                            obj2 = obj2 == null ? "" : obj2;
                            //proOrigin[i].Name != "LastModifyTime" && proOrigin[i].Name != "LastModifyPerson"
                            if (!obj1.Equals(obj2) && proOrigin[i].Name != "FormCompleteLogo" && proOrigin[i].Name != "FormCompleteTime"
                                && proOrigin[i].Name != "SubmitLogo" && proOrigin[i].Name != "SubmitTime" && proOrigin[i].Name != "MedicalStateCode"
                                && proOrigin[i].Name != "OutStationCode" && proOrigin[i].Name != "AgentCode"
                                && proOrigin[i].Name != "AgentWorkID" && proOrigin[i].Name != "AgentName" && proOrigin[i].Name != "CenterIFSpotChecks"
                                && proOrigin[i].Name != "BeginFillPatientTime" && proOrigin[i].Name != "SubCenterIFSpotChecks"
                                && proOrigin[i].Name != "MedicalRecordGenerationTime" && proOrigin[i].Name != "ChargeOrder"
                                && proOrigin[i].Name != "SubCenterPerson" && proOrigin[i].Name != "SubCenterSpotChecksTime"
                                && proOrigin[i].Name != "SubCenterSpotChecksRmark" && proOrigin[i].Name != "SubCenterSpotChecksResult"
                                && proOrigin[i].Name != "SubCenterSpotChecksUnqualifiedReason" && proOrigin[i].Name != "CenterSpotChecksPerson"
                                && proOrigin[i].Name != "CenterSpotChecksTime" && proOrigin[i].Name != "CenterSpotChecksResult"
                                && proOrigin[i].Name != "CenterSpotChecksRmark" && proOrigin[i].Name != "CenterSpotChecksUnqualifiedReason"
                                && proOrigin[i].Name != "IFMedicalHistoryWritingProgress" && proOrigin[i].Name != "IFSubCenterMedicalHistoryWritingProgress"
                                && proOrigin[i].Name != "LastUpdatePerson" && proOrigin[i].Name != "LastUpdateTime")
                            {
                                j = j + 1;
                                M_PatientRecordTrace crackInfo = new M_PatientRecordTrace();
                                var query = from a in db.M_PatientRecordTrace
                                            select a.ID;
                                if (query.Count() > 0 && j == 1)
                                {
                                    crackInfo.ID = query.Max() + 1;
                                    j = crackInfo.ID;
                                }
                                else if (j > 1)
                                { crackInfo.ID = j; }
                                else
                                { crackInfo.ID = 1; }
                                crackInfo.ModifyTime = modifyTime;//修改时间
                                crackInfo.ModifyPerson = modifyPerson;//修改人
                                crackInfo.TaskCode = pInfo.TaskCode;//任务编码
                                crackInfo.PatientOrder = pInfo.PatientOrder;//序号
                                crackInfo.ModifyItem = GetDescriptionByTableName("M_PatientRecord", proOrigin[i].Name);//修改项
                                crackInfo.OriginalValue = obj1.ToString();//原值
                                crackInfo.NewValue = obj2.ToString();//新值
                                crackInfo.OwnershipType = 1;
                                crackList.Add(crackInfo);
                            }
                        }
                        #endregion

                        #region 如果病历提交插入病历附表--体检等修改痕迹
                        if (pra != null)
                        {
                            //获得所有property的信息
                            PropertyInfo[] proOrigin1 = praInfo.GetType().GetProperties();
                            //PropertyInfo[] proNew1 = pra.GetType().GetProperties();
                            for (int i = 0; i < proOrigin1.Length; i++)
                            {
                                object obj1 = proOrigin1[i].GetValue(praInfo, null);
                                object obj2 = proOrigin1[i].GetValue(pra, null);
                                obj1 = obj1 == null ? "" : obj1;
                                obj2 = obj2 == null ? "" : obj2;
                                if (!obj1.Equals(obj2) && proOrigin1[i].Name != "OpenReactionScore" && proOrigin1[i].Name != "LanguageReactionScore" && proOrigin1[i].Name != "MotionResponseScore"
                                    && proOrigin1[i].Name != "GCSScore" && proOrigin1[i].Name != "PositionScore" && proOrigin1[i].Name != "DamageWayScore"
                                    && proOrigin1[i].Name != "CirculationChangeScore" && proOrigin1[i].Name != "BreathingChangeScore" && proOrigin1[i].Name != "ConsciousnessChangeScore"
                                    && proOrigin1[i].Name != "TIScore" && proOrigin1[i].Name != "HeartRatePerMinuteScore" && proOrigin1[i].Name != "BreathingScore"
                                    && proOrigin1[i].Name != "MuscleTensionScore" && proOrigin1[i].Name != "LaryngealReflexScore" && proOrigin1[i].Name != "SkinColorScore"
                                    && proOrigin1[i].Name != "ApgarScore" && proOrigin1[i].Name != "SymptomPendingInvestigationCodes" && proOrigin1[i].Name != "FirstImpressionCodes"
                                    && proOrigin1[i].Name != "ECGImpressionCodes" && proOrigin1[i].Name != "ECGImpressionRetestICodes" && proOrigin1[i].Name != "ECGImpressionRetestIICodes")
                                {
                                    j = j + 1;
                                    M_PatientRecordTrace crackInfo = new M_PatientRecordTrace();
                                    var query = from a in db.M_PatientRecordTrace
                                                select a.ID;
                                    if (query.Count() > 0 && j == 1)
                                    {
                                        crackInfo.ID = query.Max() + 1;
                                        j = crackInfo.ID;
                                    }
                                    else if (j > 1)
                                    { crackInfo.ID = j; }
                                    else
                                    { crackInfo.ID = 1; }
                                    crackInfo.ModifyTime = modifyTime;//修改时间
                                    crackInfo.ModifyPerson = modifyPerson;//修改人
                                    crackInfo.TaskCode = pra.TaskCode;//任务编码
                                    crackInfo.PatientOrder = pra.PatientOrder;//序号
                                    crackInfo.ModifyItem = GetDescriptionByTableName("M_PatientRecordAppend", proOrigin1[i].Name);//修改项
                                    crackInfo.OriginalValue = obj1.ToString();//原值
                                    crackInfo.NewValue = obj2.ToString();//新值
                                    crackInfo.OwnershipType = 2;
                                    crackList.Add(crackInfo);
                                }
                            }
                        }
                        #endregion

                        #region 如果病历提交插入病历附表--心肺复苏
                        if (prCPR != null)
                        {
                            //获得所有property的信息
                            PropertyInfo[] proOrigin2 = prCPRInfo.GetType().GetProperties();
                            //PropertyInfo[] proNew2 = prCPR.GetType().GetProperties();
                            for (int i = 0; i < proOrigin2.Length; i++)
                            {
                                object obj1 = proOrigin2[i].GetValue(prCPRInfo, null);
                                object obj2 = proOrigin2[i].GetValue(prCPR, null);
                                obj1 = obj1 == null ? "" : obj1;
                                obj2 = obj2 == null ? "" : obj2;
                                if (!obj1.Equals(obj2))
                                {
                                    j = j + 1;
                                    M_PatientRecordTrace crackInfo = new M_PatientRecordTrace();
                                    var query = from a in db.M_PatientRecordTrace
                                                select a.ID;
                                    if (query.Count() > 0 && j == 1)
                                    {
                                        crackInfo.ID = query.Max() + 1;
                                        j = crackInfo.ID;
                                    }
                                    else if (j > 1)
                                    { crackInfo.ID = j; }
                                    else
                                    { crackInfo.ID = 1; }
                                    crackInfo.ModifyTime = modifyTime;//修改时间
                                    crackInfo.ModifyPerson = modifyPerson;//修改人
                                    crackInfo.TaskCode = prCPRInfo.TaskCode;//任务编码
                                    crackInfo.PatientOrder = prCPRInfo.PatientOrder;//序号
                                    crackInfo.ModifyItem = GetDescriptionByTableName("M_PatientRecordCPR", proOrigin2[i].Name);//修改项
                                    crackInfo.OriginalValue = obj1.ToString();//原值
                                    crackInfo.NewValue = obj2.ToString();//新值
                                    crackInfo.OwnershipType = 3;
                                    crackList.Add(crackInfo);
                                }
                            }
                        }
                        #endregion

                    }
                    #endregion

                    if (crackList.Count > 0)
                    {
                        db.M_PatientRecordTrace.AddRange(crackList);//插入病历修改记录表
                    }

                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/Update(PC)", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #region 修改病历--救治记录主表和子表
        public bool UpdatePRRescue(M_PatientRecordRescue prrInfo, List<M_PatientRecordMeasure> measureSCO
            , List<M_PatientRecordDrug> drugSCO, List<M_PatientRecordSanitation> sanitationSCO
            , List<M_PatientRecordLossDrug> lossDrugSCO, List<M_PatientRecordLossSanitation> lossSanitationSCO)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    List<M_PatientRecordTrace> crackList = new List<M_PatientRecordTrace>();//病历修改记录
                    int j = 0;//病历修改记录主键
                    DateTime modifyTime = DateTime.Now;//修改时间
                    string modifyPerson = prrInfo.LastUpdatePerson;//修改人
                    M_PatientRecord PRoriginInfo = dalContext.IM_PatientRecordDAL.GetModelWithOutTrace(a => a.PatientOrder == prrInfo.PatientOrder && a.TaskCode == prrInfo.TaskCode);

                    string RescueRecordCode = "";
                    var entry = db.Entry(prrInfo);
                    bool origin = false;
                    if (entry.State == EntityState.Detached)
                    {
                        //if ((PRoriginInfo.SubmitLogo == true && superRole == "SuperRole") || PRoriginInfo.SubmitLogo == false)
                        //{
                            M_PatientRecordRescue originInfo = dalContext.IM_PatientRecordRescueDAL.GetModelWithOutTrace(a => a.TaskCode == prrInfo.TaskCode && a.PatientOrder == prrInfo.PatientOrder && a.RescueRecordCode == prrInfo.RescueRecordCode);
                            //如果已经被上下文追踪
                            if (originInfo != null)
                            {
                                origin = true;
                                RescueRecordCode = originInfo.RescueRecordCode;
                                prrInfo.RescueXFFSMouldChoose = originInfo.RescueXFFSMouldChoose;//心肺复苏模板选择
                                db.M_PatientRecordRescue.Attach(prrInfo);//将给定实体附加到集的基础上下文中，将实体以“未更改”的状态放置到上下文；
                                db.Entry(prrInfo).State = EntityState.Modified;


                                #region 提交病历后修改病历痕迹
                                int flag = 0;//病历状态：0.普通保存(还没有提交)；1.提交；2.提交后的保存
                                //提交后保存的
                                if (PRoriginInfo != null)
                                {
                                    if (PRoriginInfo.MedicalStateCode >0)
                                    {
                                        flag = 2;//提交后的保存
                                        #region 插入病历救治记录痕迹
                                        //获得所有property的信息
                                        PropertyInfo[] proOrigin = originInfo.GetType().GetProperties();
                                        PropertyInfo[] proNew = prrInfo.GetType().GetProperties();
                                        for (int i = 0; i < proOrigin.Length; i++)
                                        {
                                            object obj1 = proOrigin[i].GetValue(originInfo, null);
                                            object obj2 = proOrigin[i].GetValue(prrInfo, null);
                                            obj1 = obj1 == null ? "" : obj1;
                                            obj2 = obj2 == null ? "" : obj2;
                                            if (!obj1.Equals(obj2) && proOrigin[i].Name != "MeasureCodes" && proOrigin[i].Name != "DrugCodes"
                                                 && proOrigin[i].Name != "SanitationCodes" && proOrigin[i].Name != "LossDrugCodes" && proOrigin[i].Name != "LossSanitationCodes")
                                            {
                                                j = j + 1;
                                                M_PatientRecordTrace crackInfo = new M_PatientRecordTrace();
                                                var query = from a in db.M_PatientRecordTrace
                                                            select a.ID;
                                                if (query.Count() > 0 && j == 1)
                                                {
                                                    crackInfo.ID = query.Max() + 1;
                                                    j = crackInfo.ID;
                                                }
                                                else if (j > 1)
                                                { crackInfo.ID = j; }
                                                else
                                                { crackInfo.ID = 1; }
                                                crackInfo.ModifyTime = modifyTime;//修改时间
                                                crackInfo.ModifyPerson = modifyPerson;//修改人
                                                crackInfo.TaskCode = prrInfo.TaskCode;//任务编码
                                                crackInfo.PatientOrder = prrInfo.PatientOrder;//序号
                                                crackInfo.ModifyItem = GetDescriptionByTableName("M_PatientRecordRescue", proOrigin[i].Name);//修改项
                                                crackInfo.OriginalValue = obj1.ToString();//原值
                                                crackInfo.NewValue = obj2.ToString();//新值
                                                crackInfo.OwnershipType = 5;
                                                crackList.Add(crackInfo);
                                            }
                                        }
                                        #endregion
                                    }
                                }
                                #endregion
                            }
                       //}

                    }
                    if (prrInfo != null && origin == true)
                    {
                        #region 修改救治措施子表
                        var dqry0 = from infos in db.M_PatientRecordMeasure
                                    where infos.TaskCode == prrInfo.TaskCode && infos.PatientOrder == prrInfo.PatientOrder && infos.RescueRecordCode == prrInfo.RescueRecordCode
                                    select infos;
                        foreach (var inf in dqry0)
                            db.M_PatientRecordMeasure.Remove(inf);//删除救治措施子表
                        if (measureSCO != null)
                        {
                            List<M_PatientRecordMeasure> measure = new List<M_PatientRecordMeasure>();
                            for (int k = 0; k < measureSCO.Count; k++)
                            {
                                M_PatientRecordMeasure SInfo = measureSCO[k];
                                //M_PatientRecordMeasure MeasureInfo = measureSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.RescueMeasureCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;//救治记录编码
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.RescueMeasureCode = c.ID;//救治措施编码
                                SInfo.RescueMeasureName = c.Name;//救治措施
                                SInfo.OtherTypeID = c.OtherTypeID;//
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;//单价
                                int FeeScale = c.FeeScale == null ? 1 : Convert.ToInt32(c.FeeScale);
                                int Dosage = 1;
                                if (FeeScale == -1)
                                {
                                    Dosage = 1;
                                }
                                else if (FeeScale == 1)
                                {
                                    Dosage = Convert.ToInt32(SInfo.NumberOfTimes);
                                }
                                else
                                {
                                    if (FeeScale < 0 && (-FeeScale <= Convert.ToInt32(SInfo.NumberOfTimes)))
                                    {
                                        Dosage = -FeeScale;
                                    }
                                    else
                                    {
                                        Dosage = Convert.ToInt32(SInfo.NumberOfTimes);
                                    }
                                }

                                SInfo.TotalPrice = c.RealPrice * Dosage;//合计金额
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.Remark = SInfo.Remark;
                                SInfo.SelectOrder = k + 1;
                                measure.Add(SInfo);
                                //measureSCO.Add(SInfo);
                                //db.M_PatientRecordMeasure.Add(SInfo);//插入救治措施子表
                            }
                            db.M_PatientRecordMeasure.AddRange(measure);//插入救治措施子表
                        }
                        #endregion

                        #region 修改药品子表
                        var dqry1 = from infos in db.M_PatientRecordDrug
                                    where infos.TaskCode == prrInfo.TaskCode && infos.PatientOrder == prrInfo.PatientOrder && infos.RescueRecordCode == prrInfo.RescueRecordCode
                                    select infos;
                        foreach (var inf in dqry1)
                            db.M_PatientRecordDrug.Remove(inf);//删除药品子表
                        if (drugSCO != null)
                        {
                            List<M_PatientRecordDrug> drug = new List<M_PatientRecordDrug>();
                            for (int l = 0; l < drugSCO.Count; l++)
                            {
                                M_PatientRecordDrug DInfo = drugSCO[l];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == DInfo.DrugCode
                                                select m).FirstOrDefault();
                                DInfo.TaskCode = DInfo.TaskCode;
                                DInfo.PatientOrder = DInfo.PatientOrder;
                                DInfo.RescueRecordCode = RescueRecordCode;
                                DInfo.DisposeOrder = DInfo.DisposeOrder;//处理序号
                                DInfo.DrugCode = c.ID;
                                DInfo.DrugName = c.Name;
                                DInfo.GiveMedicineWay = DInfo.GiveMedicineWay;
                                string ChargeWay = "";
                                string RealPrice = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero).ToString();
                                if (c.FeeScale > 1)
                                { ChargeWay = RealPrice + "元/" + "1-" + c.FeeScale + c.TDictionary2.Name; }
                                else
                                { ChargeWay = RealPrice + "元/" + c.TDictionary2.Name; }
                                DInfo.ChargeWay = ChargeWay;//收费方式
                                DInfo.Specifications = c.Specification;//规格
                                DInfo.Unit = c.TDictionary2.Name;//单位
                                DInfo.Dosage = DInfo.Dosage;//用量
                                DInfo.Price = c.RealPrice;//单价
                                int FeeScale = c.FeeScale == null ? 1 : Convert.ToInt32(c.FeeScale);
                                int Dosage = 1;
                                double dosage = Math.Ceiling(Convert.ToDouble(DInfo.Dosage));
                                if (Convert.ToInt32(dosage) % FeeScale > 0)
                                {
                                    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(dosage / FeeScale), 0)) + 1;
                                }
                                else
                                {
                                    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(dosage / FeeScale), 0));
                                }
                                //if (Convert.ToInt32(DInfo.Dosage) % FeeScale > 0)
                                //{
                                //    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(DInfo.Dosage / FeeScale), 0)) + 1;
                                //}
                                //else
                                //{
                                //    Dosage = Convert.ToInt32(Math.Round(Convert.ToDouble(DInfo.Dosage / FeeScale), 0));
                                //}
                                DInfo.TotalPrice = c.RealPrice * Dosage;//合计金额
                                DInfo.TotalDose = DInfo.TotalDose;//合计用量
                                DInfo.Remark = DInfo.Remark;//备注
                                DInfo.SelectOrder = l + 1;//选择序号
                                DInfo.FeeScale = c.FeeScale;
                                drug.Add(DInfo);
                                //db.M_PatientRecordDrug.Add(DInfo);//插入药品子表
                            }
                            db.M_PatientRecordDrug.AddRange(drug);//插入药品子表
                        }
                        #endregion

                        #region 修改耗材子表
                        var dqry2 = from infos in db.M_PatientRecordSanitation
                                    where infos.TaskCode == prrInfo.TaskCode && infos.PatientOrder == prrInfo.PatientOrder && infos.RescueRecordCode == prrInfo.RescueRecordCode
                                    select infos;
                        foreach (var inf in dqry2)
                            db.M_PatientRecordSanitation.Remove(inf);//删除耗材子表
                        if (sanitationSCO != null)
                        {
                            List<M_PatientRecordSanitation> sanitation = new List<M_PatientRecordSanitation>();
                            for (int k = 0; k < sanitationSCO.Count; k++)
                            {
                                M_PatientRecordSanitation SInfo = sanitationSCO[k];
                                //M_PatientRecordSanitation SInfo = null;
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.SanitationCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.SanitationCode = c.ID;
                                SInfo.SanitationName = c.Name;
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;
                                SInfo.TotalPrice = c.RealPrice * SInfo.NumberOfTimes;//合计金额
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.SelectOrder = k + 1;
                                sanitation.Add(SInfo);
                            }
                            db.M_PatientRecordSanitation.AddRange(sanitation);//插入耗材子表
                        }
                        #endregion

                        #region 修改损耗药品子表
                        var dqry3 = from infos in db.M_PatientRecordLossDrug
                                    where infos.TaskCode == prrInfo.TaskCode && infos.PatientOrder == prrInfo.PatientOrder && infos.RescueRecordCode == prrInfo.RescueRecordCode
                                    select infos;
                        foreach (var inf in dqry3)
                            db.M_PatientRecordLossDrug.Remove(inf);//删除损耗药品子表
                        if (lossDrugSCO != null)
                        {
                            List<M_PatientRecordLossDrug> lossDrug = new List<M_PatientRecordLossDrug>();
                            for (int k = 0; k < lossDrugSCO.Count; k++)
                            {
                                M_PatientRecordLossDrug SInfo = lossDrugSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.DrugCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;
                                SInfo.DisposeOrder = SInfo.DisposeOrder;
                                SInfo.DrugCode = c.ID;
                                SInfo.DrugName = c.Name;//药品名称
                                SInfo.GiveMedicineWay = SInfo.GiveMedicineWay;//应该是空
                                string ChargeWay = "";
                                string RealPrice = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero).ToString();
                                if (c.FeeScale > 1)
                                { ChargeWay = RealPrice + "元/" + "1-" + c.FeeScale + c.TDictionary2.Name; }
                                else
                                { ChargeWay = RealPrice + "元/" + c.TDictionary2.Name; }
                                SInfo.ChargeWay = ChargeWay;//收费方式
                                SInfo.Specifications = c.Specification;//规格
                                SInfo.Unit = c.TDictionary2.Name;//单位
                                SInfo.Dosage = SInfo.Dosage;//用量
                                SInfo.Price = c.RealPrice;//单价
                                SInfo.TotalPrice = c.RealPrice * SInfo.Dosage;//合计金额
                                SInfo.TotalDose = SInfo.TotalDose;//合计用量
                                SInfo.Remark = SInfo.Remark;//备注
                                SInfo.SelectOrder = k + 1;
                                lossDrug.Add(SInfo);
                            }
                            db.M_PatientRecordLossDrug.AddRange(lossDrug);//插入损耗药品子表
                        }
                        #endregion

                        #region 修改损耗药品子表
                        var dqry4 = from infos in db.M_PatientRecordLossSanitation
                                    where infos.TaskCode == prrInfo.TaskCode && infos.PatientOrder == prrInfo.PatientOrder && infos.RescueRecordCode == prrInfo.RescueRecordCode
                                    select infos;
                        foreach (var inf in dqry4)
                            db.M_PatientRecordLossSanitation.Remove(inf);//删除损耗药品子表
                        if (lossSanitationSCO != null)
                        {
                            List<M_PatientRecordLossSanitation> lossSanitation = new List<M_PatientRecordLossSanitation>();
                            for (int k = 0; k < lossSanitationSCO.Count; k++)
                            {
                                M_PatientRecordLossSanitation SInfo = lossSanitationSCO[k];
                                I_Material c = (from m in db.I_Material
                                                where m.ID == SInfo.SanitationCode
                                                select m).FirstOrDefault();
                                SInfo.TaskCode = SInfo.TaskCode;
                                SInfo.PatientOrder = SInfo.PatientOrder;
                                SInfo.RescueRecordCode = RescueRecordCode;//救治记录编码
                                SInfo.DisposeOrder = SInfo.DisposeOrder;//处理序号
                                SInfo.SanitationCode = c.ID;
                                SInfo.SanitationName = c.Name;
                                SInfo.NumberOfTimes = SInfo.NumberOfTimes;//次数
                                SInfo.Price = c.RealPrice;
                                SInfo.ChargeWay = Decimal.Round(decimal.Parse(c.RealPrice.ToString()), 2, MidpointRounding.AwayFromZero) + "元/" + c.TDictionary2.Name;//收费方式
                                SInfo.TotalPrice = c.RealPrice * SInfo.NumberOfTimes;//合计金额
                                SInfo.Remark = SInfo.Remark;
                                SInfo.SelectOrder = k + 1;
                                lossSanitation.Add(SInfo);
                            }
                            db.M_PatientRecordLossSanitation.AddRange(lossSanitation);//插入损耗卫生材料子表
                        }
                        #endregion
                    }

                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/UpdatePRRescue()", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #region 删除

        #region 删除病历主表信息和附表
        public bool Delete(string TaskCode, int PatientOrder)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    M_PatientRecord originInfo = db.M_PatientRecord.FirstOrDefault(a => a.PatientOrder == PatientOrder && a.TaskCode == TaskCode);
                    db.M_PatientRecord.Remove(originInfo);//删除病历主表信息

                    M_PatientRecordAppend pra = db.M_PatientRecordAppend.FirstOrDefault(a => a.PatientOrder == PatientOrder && a.TaskCode == TaskCode);
                    if (pra != null)
                        db.M_PatientRecordAppend.Remove(pra);//删除病历附表信息--体检等

                    M_PatientRecordCPR prCPR = db.M_PatientRecordCPR.FirstOrDefault(a => a.PatientOrder == PatientOrder && a.TaskCode == TaskCode);
                    if (prCPR != null)
                        db.M_PatientRecordCPR.Remove(prCPR);//删除病历附表信息--心肺复苏

                    var dqry0 = from infos in db.M_PatientRecordDiag
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry0)
                        db.M_PatientRecordDiag.Remove(inf);//删除初步诊断子表
                    //db.M_PatientRecordDiag.RemoveRange(prDiag);//删除初步诊断子表

                    var dqry1 = from infos in db.M_PatientRecordECGImpressions
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry1)
                        db.M_PatientRecordECGImpressions.Remove(inf);//删除心电图印象
                    //db.M_PatientRecordECGImpressions.RemoveRange(prECG);//删除心电图印象

                    //删除救治记录主表和相关子表--20161123
                    //M_PatientRecordRescue prr = db.M_PatientRecordRescue.FirstOrDefault(a => a.PatientOrder == PatientOrder && a.TaskCode == TaskCode);
                    //if (prr != null)
                    var dqry2 = from infos in db.M_PatientRecordRescue
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf2 in dqry2)
                        db.M_PatientRecordRescue.Remove(inf2);//删除救治记录主表

                    var dqry3 = from infos in db.M_PatientRecordMeasure
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry3)
                        db.M_PatientRecordMeasure.Remove(inf);//删除救治记录--救治措施子表

                    var dqry4 = from infos in db.M_PatientRecordDrug
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry4)
                        db.M_PatientRecordDrug.Remove(inf);//删除救治记录--药品子表

                    var dqry5 = from infos in db.M_PatientRecordSanitation
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry5)
                        db.M_PatientRecordSanitation.Remove(inf);//删除救治记录--耗材子表

                    var dqry6 = from infos in db.M_PatientRecordLossDrug
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry6)
                        db.M_PatientRecordLossDrug.Remove(inf);//删除救治记录--损耗药品子表

                    var dqry7 = from infos in db.M_PatientRecordLossSanitation
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder
                                select infos;
                    foreach (var inf in dqry7)
                        db.M_PatientRecordLossSanitation.Remove(inf);//删除救治记录--损耗耗材子表

                    //2019-01-28 zch
                    //var dqryCharge = from infos in db.M_PatientCharge
                    //            where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.IMEI!="PAD"
                    //            select infos;
                    //foreach (var inf in dqryCharge)
                    //    db.M_PatientCharge.Remove(inf);//删除收费记录


                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/Delete()", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #region 删除病历--救治记录和子表
        public bool DeletePRRescue(string TaskCode, int PatientOrder, string RescueRecordCode, int DisposeOrder)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    //删除救治记录主表和相关子表
                    M_PatientRecordRescue prr = db.M_PatientRecordRescue.FirstOrDefault(a => a.PatientOrder == PatientOrder && a.TaskCode == TaskCode && a.RescueRecordCode == RescueRecordCode);
                    if (prr != null)
                        db.M_PatientRecordRescue.Remove(prr);//删除救治记录主表

                    var dqry3 = from infos in db.M_PatientRecordMeasure
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.RescueRecordCode == RescueRecordCode
                                select infos;
                    foreach (var inf in dqry3)
                        db.M_PatientRecordMeasure.Remove(inf);//删除救治记录--救治措施子表

                    var dqry4 = from infos in db.M_PatientRecordDrug
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.RescueRecordCode == RescueRecordCode
                                select infos;
                    foreach (var inf in dqry4)
                        db.M_PatientRecordDrug.Remove(inf);//删除救治记录--药品子表

                    var dqry5 = from infos in db.M_PatientRecordSanitation
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.RescueRecordCode == RescueRecordCode
                                select infos;
                    foreach (var inf in dqry5)
                        db.M_PatientRecordSanitation.Remove(inf);//删除救治记录--耗材子表

                    var dqry6 = from infos in db.M_PatientRecordLossDrug
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.RescueRecordCode == RescueRecordCode
                                select infos;
                    foreach (var inf in dqry6)
                        db.M_PatientRecordLossDrug.Remove(inf);//删除救治记录--损耗药品子表

                    var dqry7 = from infos in db.M_PatientRecordLossSanitation
                                where infos.TaskCode == TaskCode && infos.PatientOrder == PatientOrder && infos.RescueRecordCode == RescueRecordCode
                                select infos;
                    foreach (var inf in dqry7)
                        db.M_PatientRecordLossSanitation.Remove(inf);//删除救治记录--损耗耗材子表

                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/DeletePRRescue()", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #endregion

        #region 中心心肺复苏审核
        public bool UpdateAuditCPR(M_PatientRecordCPR info)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    M_PatientRecordCPR prCPRInfo = dalContext.IM_PatientRecordCPRDAL.GetModelWithOutTrace(a => a.PatientOrder == info.PatientOrder && a.TaskCode == info.TaskCode);
                    if (prCPRInfo != null)
                    {
                        info.Witness = prCPRInfo.Witness;
                        info.CarToBeforeCPR = prCPRInfo.CarToBeforeCPR;
                        info.CarToBeforeDefibrillation = prCPRInfo.CarToBeforeDefibrillation;
                        info.EmergencyTime = prCPRInfo.EmergencyTime;
                        info.ECGMonitoringTime = prCPRInfo.ECGMonitoringTime;
                        info.CardiacArrestReason = prCPRInfo.CardiacArrestReason;
                        info.CardiacArrestReasonSupplement = prCPRInfo.CardiacArrestReasonSupplement;
                        info.BeforeResuscitationECGDiagnosis = prCPRInfo.BeforeResuscitationECGDiagnosis;
                        info.BeforeResuscitationSaO2 = prCPRInfo.BeforeResuscitationSaO2;
                        info.AfterResuscitationBP = prCPRInfo.AfterResuscitationBP;
                        info.AfterResuscitationSaO2 = prCPRInfo.AfterResuscitationSaO2;
                        info.PulsationAppearTime = prCPRInfo.PulsationAppearTime;
                        info.BreatheAppearTime = prCPRInfo.BreatheAppearTime;
                        info.AfterResuscitationECGDiagnosis = prCPRInfo.AfterResuscitationECGDiagnosis;
                        info.IFAdmittedToHospital = prCPRInfo.IFAdmittedToHospital;
                        info.DoctorFollowUp = prCPRInfo.DoctorFollowUp;//20160419-回访补充
                        info.DoctorFollowUpReason = prCPRInfo.DoctorFollowUpReason;//20160419-回访原因
                        info.AttackPlace = prCPRInfo.AttackPlace;//20160419-发病地点
                        info.ArriveSceneTime = prCPRInfo.ArriveSceneTime;//20160419-到达现场时间
                        info.ChestCompression = prCPRInfo.ChestCompression;//20160419-胸外按压
                        info.StartChestCompressionTime = prCPRInfo.StartChestCompressionTime;//20160419-开始胸外按压时间
                        info.Defibrillation = prCPRInfo.Defibrillation;//20160419-除颤/同步电复律
                        info.FirstDefibrillationTime = prCPRInfo.FirstDefibrillationTime;//20160419-首次除颤/电复律时间
                        info.IntervalTime = prCPRInfo.IntervalTime;//20160419-间隔时间
                        info.WitnessHeartbeatBreatheArrest = prCPRInfo.WitnessHeartbeatBreatheArrest;//20160419-目击下心跳呼吸骤停

                        db.M_PatientRecordCPR.Attach(info);//将给定实体附加到集的基础上下文中，将实体以“未更改”的状态放置到上下文；
                        db.Entry(info).State = EntityState.Modified;//修改病历附表--心肺复苏
                    }
                    db.SaveChanges();//将在此上下文中所做的所有更改保存到基础数据库
                    tran.Commit(); //提交事务
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/UpdateAuditCPR()", ex.ToString());
                    if (tran != null)
                        tran.Rollback();  //回滚事务
                    return false;
                }
            }
        }
        #endregion

        #region 更新抽查病历
        public bool UpdateSpotChecks(M_PatientRecord info, int orderNumber)
        {
            try
            {
                StringBuilder strSQL = new StringBuilder();
                switch (orderNumber)
                {
                    case -1://分中心抽查
                        strSQL.Append(@"update M_PatientRecord set SubCenterIFSpotChecks='" + info.SubCenterIFSpotChecks + "',SubCenterPerson='" + info.SubCenterPerson + "',SubCenterSpotChecksTime='" + info.SubCenterSpotChecksTime + "',SubCenterSpotChecksResult='" + info.SubCenterSpotChecksResult + "' ");
                        strSQL.Append(@" ,SubCenterSpotChecksUnqualifiedReason='" + info.SubCenterSpotChecksUnqualifiedReason + "'");
                        strSQL.Append(@" ,SubCenterSpotChecksRmark='" + info.SubCenterSpotChecksRmark + "' ");

                        if (info.SubCenterSpotChecksResult == "未通过")
                        {
                            strSQL.Append(@" ,MedicalStateCode=2 ");//20161212--状态修改为未通过
                        }
                        strSQL.Append(@" 
                                    where TaskCode='" + info.TaskCode + "' and PatientOrder=" + info.PatientOrder + "  ");
                        break;
                    case -2://中心抽查
                        strSQL.Append(@"update M_PatientRecord set CenterIFSpotChecks='" + info.CenterIFSpotChecks + "',CenterSpotChecksPerson='" + info.CenterSpotChecksPerson + "',CenterSpotChecksTime='" + info.CenterSpotChecksTime + "',CenterSpotChecksResult='" + info.CenterSpotChecksResult + "' ");
                        strSQL.Append(@" ,CenterSpotChecksUnqualifiedReason='" + info.CenterSpotChecksUnqualifiedReason + "'");

                        strSQL.Append(@" ,IFMedicalHistoryWritingProgress='" + info.IFMedicalHistoryWritingProgress + "'");
                        strSQL.Append(@" ,CenterSpotChecksRmark='" + info.CenterSpotChecksRmark + "' ");
                        if (Convert.ToString(info.IFNoticeAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,IFNoticeAttendNursingLectures='" + info.IFNoticeAttendNursingLectures + "'");
                            strSQL.Append(@" ,NoticeAttendTime='" + info.NoticeAttendTime + "' ");//20151208
                            strSQL.Append(@" ,NoticeAttendPerson='" + info.NoticeAttendPerson + "' ");//20151208
                        }
                        else
                        {
                            strSQL.Append(@" ,IFNoticeAttendNursingLectures=null");
                            strSQL.Append(@" ,NoticeAttendTime=null ");//20151208
                            strSQL.Append(@" ,NoticeAttendPerson=null ");//20151208
                        }
                        if (Convert.ToString(info.ActualAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,ActualAttendNursingLectures='" + info.ActualAttendNursingLectures + "'");
                            strSQL.Append(@" ,ActualAttendTime='" + info.ActualAttendTime + "' ");//20151208
                            strSQL.Append(@" ,ActualAttendPerson='" + info.ActualAttendPerson + "' ");//20151208
                        }
                        else
                        {
                            strSQL.Append(@" ,ActualAttendNursingLectures=null");
                            strSQL.Append(@" ,ActualAttendTime=null ");//20151208
                            strSQL.Append(@" ,ActualAttendPerson=null ");//20151208
                        }
                        if (Convert.ToString(info.IFFillNoticeAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,IFFillNoticeAttendNursingLectures='" + info.IFFillNoticeAttendNursingLectures + "'");
                            strSQL.Append(@" ,FillNoticeAttendTime='" + info.FillNoticeAttendTime + "' ");//20151208
                            strSQL.Append(@" ,FillNoticeAttendPerson='" + info.FillNoticeAttendPerson + "' ");//20151208
                        }
                        else
                        {
                            strSQL.Append(@" ,IFFillNoticeAttendNursingLectures=null");
                            strSQL.Append(@" ,FillNoticeAttendTime=null ");//20151208
                            strSQL.Append(@" ,FillNoticeAttendPerson=null ");//20151208
                        }
                        if (Convert.ToString(info.FillActualAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,FillActualAttendNursingLectures='" + info.FillActualAttendNursingLectures + "'");
                            strSQL.Append(@" ,FillActualAttendTime='" + info.FillNoticeAttendTime + "' ");//20151208
                            strSQL.Append(@" ,FillActualAttendPerson='" + info.FillActualAttendPerson + "' ");//20151208
                        }
                        else
                        {
                            strSQL.Append(@" ,FillActualAttendNursingLectures=null");
                            strSQL.Append(@" ,FillActualAttendTime=null ");//20151208
                            strSQL.Append(@" ,FillActualAttendPerson=null ");//20151208
                        }

                        if (Convert.ToString(info.IFFill2NoticeAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,IFFill2NoticeAttendNursingLectures='" + info.IFFill2NoticeAttendNursingLectures + "'");
                            strSQL.Append(@" ,Fill2NoticeAttendTime='" + info.Fill2NoticeAttendTime + "' ");//20151209
                            strSQL.Append(@" ,Fill2NoticeAttendPerson='" + info.Fill2NoticeAttendPerson + "' ");//20151209
                        }
                        else
                        {
                            strSQL.Append(@" ,IFFill2NoticeAttendNursingLectures=null");
                            strSQL.Append(@" ,Fill2NoticeAttendTime=null ");//20151209
                            strSQL.Append(@" ,Fill2NoticeAttendPerson=null ");//20151209
                        }
                        if (Convert.ToString(info.Fill2ActualAttendNursingLectures) != "")
                        {
                            strSQL.Append(@" ,Fill2ActualAttendNursingLectures='" + info.Fill2ActualAttendNursingLectures + "'");
                            strSQL.Append(@" ,Fill2ActualAttendTime='" + info.Fill2NoticeAttendTime + "' ");//20151209
                            strSQL.Append(@" ,Fill2ActualAttendPerson='" + info.Fill2ActualAttendPerson + "' ");//20151209
                        }
                        else
                        {
                            strSQL.Append(@" ,Fill2ActualAttendNursingLectures=null");
                            strSQL.Append(@" ,Fill2ActualAttendTime=null ");//20151209
                            strSQL.Append(@" ,Fill2ActualAttendPerson=null ");//20151209
                        }
                        if (info.CenterSpotChecksResult == "未通过")
                        {
                            strSQL.Append(@" ,MedicalStateCode=2 ");//20161129--状态修改为未通过
                        }
                        
                        strSQL.Append(@" 
                                    where TaskCode='" + info.TaskCode + "' and PatientOrder=" + info.PatientOrder + "  ");
                        break;
                    default:
                        break;
                }

                int ds = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, strSQL.ToString(), null);
                return ds > 0 ? true : false;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/UpdateSpotChecks()", ex.ToString());
                return false;
            }
        }
        #endregion

        #region 更新回访
        public bool UpdateFollowUp(string TaskCode, int PatientOrder, string FollowUpState, string FollowUpReason, string FollowUpSupplement)
        {
            try
            {
                StringBuilder strSQL = new StringBuilder();
                if (FollowUpState == "2")
                {
                    strSQL.Append(@"update M_PatientRecord set CenterFollowUp='" + FollowUpReason + "',CenterFollowUpSupplement='" + FollowUpSupplement + "' ");
                    strSQL.Append(@" where TaskCode='" + TaskCode + "' and PatientOrder=" + PatientOrder + "  ");
                }
                else
                {
                    strSQL.Append(@"update M_PatientRecordCPR set DoctorFollowUpReason='" + FollowUpReason + "',DoctorFollowUp='" + FollowUpSupplement + "' ");
                    strSQL.Append(@" where TaskCode='" + TaskCode + "' and PatientOrder=" + PatientOrder + "  ");
                }

                int ds = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, strSQL.ToString(), null);
                return ds > 0 ? true : false;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/UpdateFollowUp()", ex.ToString());
                return false;
            }
        }
        #endregion

        #region 根据表名来获取对应的字段说明
        public string GetDescriptionByTableName(string tableName, string FieldName)
        {
            //
            StringBuilder sbSQL = new StringBuilder();//sql语句
            sbSQL.Append(" SELECT Id=objs.name,Name =isnull(props.[value],'')");
            sbSQL.Append(" FROM syscolumns cols ");
            sbSQL.Append(" inner join sysobjects objs on cols.id= objs.id and  objs.xtype='U' and  objs.name<>'dtproperties' ");
            sbSQL.Append(" left join sys.extended_properties props on cols.id=props.major_id and cols.colid=props.minor_id ");
            sbSQL.Append(" WHERE objs.name='" + tableName + "' ");
            sbSQL.Append(" and cols.name='" + FieldName + "' ");

            var Templist = db.Database.SqlQuery<M_CheckModel>(sbSQL.ToString());
            string mDescription = Templist.FirstOrDefault().Name;
            return mDescription;

        }
        #endregion


        #region 病历自动归档后退回病历可以编辑
        public bool FormComplete(string TaskCode, int PatientOrder, string UpdateFormCompletePerson)
        {
            try
            {
                StringBuilder strSQL = new StringBuilder();
                strSQL.Append(@"update M_PatientRecord set FormCompleteLogo=null,UpdateFormCompletePerson='" + UpdateFormCompletePerson + "',UpdateFormCompleteTime='" + DateTime.Now + "' ");
                strSQL.Append(@" where TaskCode='" + TaskCode + "' and PatientOrder=" + PatientOrder + "  ");

                int ds = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, strSQL.ToString(), null);
                return ds > 0 ? true : false;
            }
            catch (Exception ex)
            {
                LogUtility.Error("M_PatientRecordDAL/FormComplete()", ex.ToString());
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 拼接指导医生姓名
        /// add2021-10-18 plq 医保上线移植
        /// </summary>
        /// <param name="taskCode"></param>
        /// <param name="patientOrder"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public static bool UpdatePatientRecord(string taskCode, int patientOrder, string personName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select top 1 处方医师姓名  FROM M_PatientRecordYiBao yb left join M_PatientRecordYiBaoMX ybmx on yb.编码 = ybmx.编码 where 任务编码 = '" + taskCode + "' and 病历序号 = '" + patientOrder + "' and 操作状态 in (3,4) ;");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.YBConnectionString, CommandType.Text, sb.ToString(), null))
                try
                {
                    String DoctorAndNurse = "";
                    if (dr.Read())
                    {
                        DoctorAndNurse = dr["处方医师姓名"].ToString();
                    }
                    if (DoctorAndNurse.Length > 0)
                    {
                        LogUtility.Debug("M_PatientRecordDAL/UpdatePatientRecord", taskCode + "," + patientOrder + "," + personName + "," + DoctorAndNurse);
                        DoctorAndNurse = DoctorAndNurse == personName ? personName : DoctorAndNurse + ',' + personName;
                        StringBuilder sb1 = new StringBuilder();
                        sb1.Append(@" update M_PatientRecord set DoctorAndNurse= '" + DoctorAndNurse + "' where TaskCode ='" + taskCode + "' and PatientOrder = '" + patientOrder + "' ");
                        int num = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb1.ToString(), null);
                        if (num > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return true;

                }
                catch (Exception ex)
                {
                    LogUtility.Error("M_PatientRecordDAL/UpdatePatientRecord", ex.Message);
                    return false;
                }
        }
    }
}
